Java REST POST 与 POSTMAN 一起正常工作,但在使用 Spring RestTemplate 时出现异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37339753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST POST works correctly with POSTMAN but exception when using Spring RestTemplate
提问by Rehan
I am writing a Rest client to post JSON data using Spring RestTemplate. Using POSTMAN and following JSON data in body get the response correctly-
我正在编写一个 Rest 客户端来使用 Spring RestTemplate 发布 JSON 数据。使用 POSTMAN 并在正文中遵循 JSON 数据可正确获得响应-
{
"InCode":"test",
"Name":"This is test",
"Email":"[email protected]",
"Id":18,
}
However when trying to hit the REST API using Spring RestTemplate as follows
但是,当尝试使用 Spring RestTemplate 访问 REST API 时,如下所示
ResponseEntity<String> response = restTemplate.exchange(baseUrl,
HttpMethod.POST, getHeaders(), String.class);
private HttpEntity<?> getHeaders() throws JSONException {
JSONObject request = new JSONObject();
request.put("Email", "[email protected]");
request.put("Id", "18");
request.put("Name", "This is test");
request.put("InCode", "test");
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
return new HttpEntity<>(request.toString(), headers);
}
I get the exception-
我得到了例外-
11:52:56.808 [main] DEBUG o.s.web.client.RestTemplate - Created POST request for "http://server-test/platform/v4/org"
11:52:56.815 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, */*]
12:03:47.357 [main] DEBUG o.s.web.client.RestTemplate - Writing [{"InCode":"test","Email":"[email protected]","Id":"18","Name":"This is test"}] using [org.springframework.http.converter.StringHttpMessageConverter@6a1aab78]
11:52:57.574 [main] DEBUG o.s.web.client.RestTemplate - POST request for "http://server-test/platform/v4/org" resulted in 500 (Internal Server Error); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
Would be thankful for any help.
将不胜感激任何帮助。
回答by Stefan Isele - prefabware.com
You are setting the 'Accept' header, which defines which content type you will accept as a response.
您正在设置“接受”标头,它定义您将接受哪种内容类型作为响应。
Instead you must set the header 'Content-Type' with 'application/json'.
相反,您必须使用“application/json”设置标题“Content-Type”。
Edit:
编辑:
In your java code id is a string, in the postman its a number. May be this makes the server fail?
在您的 java 代码中 id 是一个字符串,在邮递员中它是一个数字。可能这会使服务器失败?