Spring RestTemplate Post for Object with String 的问题
Problems with Spring RestTemplate Post for Object with String
我一直在努力解决这个问题,我找不到我做错了什么。
我正在尝试制作一个简单的 JSON Post,使用 Spring RestTemplate,并将所有信息作为 String 发送,以使其一直保持简单,如下所示:
RestTemplate restTemplate = new RestTemplate();
JSONObject issue = getJSONIssue(redmine);
try {
String response = restTemplate.postForObject(redmineProperties.getUrl()+"/issues.json?key="+redmineProperties.getKey(), issue.toJSONString(), String.class);
}catch(RestClientException e){
e.printStackTrace();
}
我的JSON对象看起来像:
{"issue":
{"priority_id":4,
"description":"asdasdasdasd",
"subject":"adasdad",
"project_id":4
}
}
这是我试图通过 Post 发送的数据,服务器一直向我发送错误:
[http-bio-8080-exec-7] WARN org.springframework.web.client.RestTemplate - POST request for "http://xxx.com.br/issues.json?key=b8143da980578fee4db0b33bd3cd3eec511797d6" resulted in 422 (Unprocessable Entity); invoking error handler
org.springframework.web.client.HttpClientErrorException: 422 Unprocessable Entity
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:589)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:547)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:503)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:331)
at br.com.cubbes.docmidia.controller.redmine.RedmineController.saveUser(RedmineController.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
当我通过 Google Chrome 之类的客户端发送相同的数据时,服务器可以毫无问题地解析。
有谁知道我在这里做错了什么?
我最终使用特定的库来操作此 REST 调用:
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_java
谢谢大家
对于为 422 Unprocessable Entity
而来的任何人,正如其中一条评论所建议的那样,设置 content-type:application/json
为我修复了错误。
RequestEntity<ValidationRequest> requestEntity = RequestEntity.post(validatorUri)
.contentType(MediaType.APPLICATION_JSON)
.body(validationRequest);
JsonNode validationResponse = restTemplate.exchange(requestEntity, JsonNode.class).getBody();
我一直在努力解决这个问题,我找不到我做错了什么。
我正在尝试制作一个简单的 JSON Post,使用 Spring RestTemplate,并将所有信息作为 String 发送,以使其一直保持简单,如下所示:
RestTemplate restTemplate = new RestTemplate();
JSONObject issue = getJSONIssue(redmine);
try {
String response = restTemplate.postForObject(redmineProperties.getUrl()+"/issues.json?key="+redmineProperties.getKey(), issue.toJSONString(), String.class);
}catch(RestClientException e){
e.printStackTrace();
}
我的JSON对象看起来像:
{"issue":
{"priority_id":4,
"description":"asdasdasdasd",
"subject":"adasdad",
"project_id":4
}
}
这是我试图通过 Post 发送的数据,服务器一直向我发送错误:
[http-bio-8080-exec-7] WARN org.springframework.web.client.RestTemplate - POST request for "http://xxx.com.br/issues.json?key=b8143da980578fee4db0b33bd3cd3eec511797d6" resulted in 422 (Unprocessable Entity); invoking error handler
org.springframework.web.client.HttpClientErrorException: 422 Unprocessable Entity
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:589)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:547)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:503)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:331)
at br.com.cubbes.docmidia.controller.redmine.RedmineController.saveUser(RedmineController.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
当我通过 Google Chrome 之类的客户端发送相同的数据时,服务器可以毫无问题地解析。
有谁知道我在这里做错了什么?
我最终使用特定的库来操作此 REST 调用:
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_java
谢谢大家
对于为 422 Unprocessable Entity
而来的任何人,正如其中一条评论所建议的那样,设置 content-type:application/json
为我修复了错误。
RequestEntity<ValidationRequest> requestEntity = RequestEntity.post(validatorUri)
.contentType(MediaType.APPLICATION_JSON)
.body(validationRequest);
JsonNode validationResponse = restTemplate.exchange(requestEntity, JsonNode.class).getBody();