【问题标题】:Problems with Spring RestTemplate Post for Object with String带有字符串的对象的 Spring RestTemplate Post 问题
【发布时间】:2015-09-10 18:04:03
【问题描述】:

我一直在努力解决这个问题,但我找不到我做错了什么。

我正在尝试使用 Spring RestTemplate 制作一个简单的 JSON Post,并将所有信息作为 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();
}

我的 JSONObject 看起来像:

{"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 的 insomnia 等客户端发送相同的数据时,服务器会毫无问题地解析。

有人知道我在这里做错了什么吗?

【问题讨论】:

  • 我想服务器期望内容类型为“application/json”。为什么不在这里设置?
  • 您能提供redmineProperties.getUrl()+"/issues.json?key="+redmineProperties.getKey() 端点处理程序源吗?可以肯定的是,问题就在那里。

标签: java json spring rest resttemplate


【解决方案1】:

对于任何来这里 422 Unprocessable Entity 的人,正如其中一个 cmets 所建议的那样,设置 content-type:application/json 为我修复了错误。

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

【讨论】:

    【解决方案2】:

    我最终使用了一个特定的库来操作这个 REST 调用:

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

    谢谢大家

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 2014-09-22
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多