简体   繁体   English

向服务器请求时如何在 RestTemplate 上获取数据 json

[英]How to get data json on RestTemplate when request to server

I'm using RestTemplate and JSONObject json to POST request to server.我正在使用 RestTemplate 和 JSONObject json 向服务器发布请求。 When I try to request on Postmen, it run success and return result 200(Ok), But when run my code it can not run当我尝试在 Postmen 上请求时,它运行成功并返回结果 200(Ok),但是当运行我的代码时它无法运行

This is my format json:这是我的格式 json:

{
 "senderUser": "user@gmail.com",
"data": [
{
  "actionType": "update-contact",
  "data": {
    "name": "luong van",
    "lastname": "khanh",
    "type": 0,
    "title": "",
    "passport": "",
    "gender": 1,
    "bgInfo": "",
    "dateOfBirth": "",
    "emails": [{"value": "user@gmail.com"}],
    "phones": [{"value": "0902032618"}],
    "addresses": [{"street": "10", "city":"Osaka", "state": "Osake", "country":       
        {"code":"JP", "name":"Japan"}}],
    "tag": ""
  }
}
 ]
}

This is my class:这是我的 class:

public class InComingAPI {
private static Logger logger = LoggerFactory.getLogger(InComingAPI.class);

public static void main(String []args) {
    RestTemplate restTemplate = new RestTemplate();

    String url = "https://myservice/90336429462601e7f3326641898fabd9948b349d";

    try {
        logger.info("hi there");
        // RestTemplate restTemplate = new RestTemplate();
        // String url = "http://localhost:8181/xyz/updateAdmin";

        JSONArray json = new JSONArray();
        JSONObject obj = new JSONObject();

        obj.put("name", "khanh");
        obj.put("lastname", "luong van");
        obj.put("type", "0");
        obj.put("dateOfBirth", "");
        obj.put("emails", "user@gmail.com");
        obj.put("phones", "0902032618");
        obj.put("addresses", "Osaka");

        json.put(obj);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> entity = new HttpEntity<String>(obj.toString(), headers);
        String result = restTemplate.postForObject(url, json, String.class);
        System.out.println(result);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
 }
}

and it happens an exception following as:它发生以下异常:

2021-01-22 17:50:17,280  INFO [com.outincoming.InComingAPI] hi there
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONArray]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:787)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:566)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:329)
at com.outincoming.InComingAPI.main(InComingAPI.java:70)

How I can fix the problem?我该如何解决这个问题? many thank多谢

Actually, the exception thrown is totally misleading.实际上,抛出的异常完全是误导性的。 Turned out the problem is not that the MappingJackson2HttpMessageConverter didn't know how to marshall my object -which sounded strange, being JSON-, but a configuration of the underlying ObjectMapper.原来问题不在于 MappingJackson2HttpMessageConverter 不知道如何编组我的 object——听起来很奇怪,是 JSON——,而是底层 ObjectMapper 的配置。

What I did is to disable the property SerializationFeature.FAIL_ON_EMPTY_BEANS like that我所做的是像那样禁用属性 SerializationFeature.FAIL_ON_EMPTY_BEANS

public class InComingAPI {
private static Logger logger = LoggerFactory.getLogger(InComingAPI.class);

public static void main(String []args) {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);

    String url = "https://myservice/90336429462601e7f3326641898fabd9948b349d";

    try {
        logger.info("hi there");
        // RestTemplate restTemplate = new RestTemplate();
        // String url = "http://localhost:8181/xyz/updateAdmin";

        JSONArray json = new JSONArray();
        JSONObject obj = new JSONObject();

        obj.put("name", "khanh");
        obj.put("lastname", "luong van");
        obj.put("type", "0");
        obj.put("dateOfBirth", "");
        obj.put("emails", "user@gmail.com");
        obj.put("phones", "0902032618");
        obj.put("addresses", "Osaka");

        json.put(obj);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> entity = new HttpEntity<String>(obj.toString(), headers);
        String result = restTemplate.postForObject(url, json, String.class);
        System.out.println(result);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM