org.springframework.web.client.httpclienterrorexception:resttemplate中有400个错误请求

bakd9h0s  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(486)

我使用rest模板发布数据json,当我测试o postmen时,它运行success并返回200。
这是我的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": ""
 }
 }
]
}

这是我的消息来源:

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.com/listeners/60011cbd4a674458d3b26025/90336429462601e7f3326641898fabd9948b349d";

    try {
        System.out.println("hi there");

        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, entity, String.class);          
        System.out.println(result);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

生成日志时出现异常:

17:07:58.483 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "https://myservice.com/listeners/60011cbd4a674458d3b26025/90336429462601e7f3326641898fabd9948b349d"
17:07:58.485 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/json, application/*+json, application/*+json, */*]
17:07:58.486 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [{"emails":"user@gmail.com","addresses":"Osaka","name":"khanh","phones":"0902032618","dateOfBirth":"","type":"0","lastname":"luong van"}] as "application/json" using [org.springframework.http.converter.StringHttpMessageConverter@3a7442c7]
17:07:58.831 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "https://myservice.com/listeners/60011cbd4a674458d3b26025/90336429462601e7f3326641898fabd9948b349d" resulted in 400 (Bad Request); invoking error handler
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380)
at com.javainuse.TestConnect.main(TestConnect.java:56)

我尝试了很多方法,但仍然发生同样的错误,我如何才能解决这个问题?非常感谢

cs7cruho

cs7cruho1#

我认为在这种情况下,错误的要求是正确的答案。在使用postman的测试中,您通过了请求主体:

{
    "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": ""
        }
    }]
}

当您的应用程序日志显示您正在发送请求正文时:

{
    "emails": "user@gmail.com",
    "addresses": "Osaka",
    "name": "khanh",
    "phones": "0902032618",
    "dateOfBirth": "",
    "type": "0",
    "lastname": "luong van"
}

除非您尝试使用的服务具有足够的通用性,可以接受太多不同的请求主体,否则它将触发错误的请求响应。
我建议您将postman的请求主体作为参数传递给httpentity构造函数作为测试:

HttpEntity<String> entity = new HttpEntity<String>("{\r\n" + 
            "   \"senderUser\": \"user@gmail.com\",\r\n" + 
            "   \"data\": [{\r\n" + 
            "       \"actionType\": \"update-contact\",\r\n" + 
            "       \"data\": {\r\n" + 
            "           \"name\": \"luong van\",\r\n" + 
            "           \"lastname\": \"khanh\",\r\n" + 
            "           \"type\": 0,\r\n" + 
            "           \"title\": \"\",\r\n" + 
            "           \"passport\": \"\",\r\n" + 
            "           \"gender\": 1,\r\n" + 
            "           \"bgInfo\": \"\",\r\n" + 
            "           \"dateOfBirth\": \"\",\r\n" + 
            "           \"emails\": [{\r\n" + 
            "               \"value\": \"user@gmail.com\"\r\n" + 
            "           }],\r\n" + 
            "           \"phones\": [{\r\n" + 
            "               \"value\": \"0902032618\"\r\n" + 
            "           }],\r\n" + 
            "           \"addresses\": [{\r\n" + 
            "               \"street\": \"10\",\r\n" + 
            "               \"city\": \"Osaka\",\r\n" + 
            "               \"state\": \"Osake\",\r\n" + 
            "               \"country\": {\r\n" + 
            "                   \"code\": \"JP\",\r\n" + 
            "                   \"name\": \"Japan\"\r\n" + 
            "               }\r\n" + 
            "           }],\r\n" + 
            "           \"tag\": \"\"\r\n" + 
            "       }\r\n" + 
            "   }]\r\n" + 
            "}", headers);
yws3nbqq

yws3nbqq2#

使用与json模式和jacksonmapper或类似工具匹配的pojo来节省时间。pojo到json,反之亦然,这并不是一个你需要对imo过于担心的问题,因为有框架级的功能和库来解决这个问题。用jsonarray和jsonobject构建它过于冗长,而且容易出现人为错误。

相关问题