簡體   English   中英

向服務器請求時如何在 RestTemplate 上獲取數據 json

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

我正在使用 RestTemplate 和 JSONObject json 向服務器發布請求。 當我嘗試在 Postmen 上請求時,它運行成功並返回結果 200(Ok),但是當運行我的代碼時它無法運行

這是我的格式 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": ""
  }
}
 ]
}

這是我的 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();
    }
 }
}

它發生以下異常:

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)

我該如何解決這個問題? 多謝

實際上,拋出的異常完全是誤導性的。 原來問題不在於 MappingJackson2HttpMessageConverter 不知道如何編組我的 object——聽起來很奇怪,是 JSON——,而是底層 ObjectMapper 的配置。

我所做的是像那樣禁用屬性 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