简体   繁体   中英

Problems with Spring RestTemplate Post for Object with String

I've been struggling to solve this issue here, and i can't find what i am doing wrong.

I am trying to make a simple JSON Post, using Spring RestTemplate, and sending all information as String to be simple all the way around as follows:

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();
}

My JSONObject looks like:

{"issue":
    {"priority_id":4,
     "description":"asdasdasdasd",
     "subject":"adasdad",
     "project_id":4
     }
 }

This is the data i am trying to sendo via Post, and the server keep sending me the error:

    [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)

when i send the same data via a client like insomnia by Google Chrome, the servers resolves without a problem.

Do anyone know what i could be doing wrong here?

I ended up using a specific library to manipulate this REST calls:

http://www.redmine.org/projects/redmine/wiki/Rest_api_with_java

Thanks guys

For anyone coming here for 422 Unprocessable Entity , as suggested in one of the comments, setting the content-type:application/json fixed the error for me.

RequestEntity<ValidationRequest> requestEntity = RequestEntity.post(validatorUri)
            .contentType(MediaType.APPLICATION_JSON)
            .body(validationRequest);
JsonNode validationResponse = restTemplate.exchange(requestEntity, JsonNode.class).getBody();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM