Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过RestTemplate发布JSON对象导致响应400错误的请求_Java_Json_Spring_Post_Resttemplate - Fatal编程技术网

Java 通过RestTemplate发布JSON对象导致响应400错误的请求

Java 通过RestTemplate发布JSON对象导致响应400错误的请求,java,json,spring,post,resttemplate,Java,Json,Spring,Post,Resttemplate,我试图将带有JSON数据的POST请求发送到某个Swagger API,但它会导致错误: {“错误”:{“消息”:“接收到无效的json消息”,“状态”:400} 代码如下: RestTemplate restTemplate = new RestTemplate(); List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); MappingJackson2HttpMessageC

我试图将带有JSON数据的POST请求发送到某个Swagger API,但它会导致错误:
{“错误”:{“消息”:“接收到无效的json消息”,“状态”:400}

代码如下:

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
messageConverters.add(converter);
restTemplate.setMessageConverters(messageConverters);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

String date = "05/13/2019";

JSONObject jsonObject = new JSONObject();
jsonObject.put("startDate", date);
jsonObject.put("endDate", date);

HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(),headers);

String url = "https://api/reports/";

ResponseEntity<List> response = restTemplate.postForEntity(url,entity,List.class);
但当我通过curl发送请求时:

curl -X POST "https://api/reports/" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"endDate\":\"05/13/2019\",\"startDate\":\"05/13/2019\"}"
很好用

当我尝试发送JSONObject时,如:

HttpEntity<JSONObject> entity = new HttpEntity<>(jsonObject,headers);

我做错了什么?请帮助。

在使用CharlesProxy检查请求后,我找到了答案

当我将
jsonObject
作为
字符串传递时:

HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(),headers);
正如我们看到的,它不是正确的JSON数据,然后我将
jsonObject
作为
Map
传递:

HttpEntity<Map> entity = new HttpEntity<>(jsonObject.toMap(),headers);

给我带来200个OK响应

你的招摇过市api收到了什么?@AmerQarabsa我不知道,因为它是远程api你不知道你调用的api需要什么?你应该能看穿这一切swagger@AmerQarabsaAPI只需要两个字符串参数:startDate和endDate
HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(),headers);
"{\"endDate\":\"05/13/2019\",\"startDate\":\"05/13/2019\"}"
HttpEntity<Map> entity = new HttpEntity<>(jsonObject.toMap(),headers);
{
    "endDate": "05/13/2019",
    "startDate": "05/13/2019"
}