【问题标题】:Spring REST Jackson - StringBuilder Is Not Binding With JSONSpring REST Jackson - StringBuilder 不与 JSON 绑定
【发布时间】:2017-04-19 13:19:11
【问题描述】:

我正在使用 Spring MVC RESTful webservices & Client 是 RestTemplate。通过使用杰克逊将 json 绑定到 bean,一切正常,但是当我在 bean 中添加 StringBuilder 属性时,客户端低于异常

org.springframework.web.client.ResourceAccessException:对“http://”的 POST 请求出现 I/O 错误:服务器返回 HTTP 响应代码:URL 为 415:http:// 嵌套异常是 java。 io.IOException:服务器返回 HTTP 响应代码:415 用于 URL:http://

原因:java.io.IOException:服务器返回 HTTP 响应代码:415 用于 URL:http://

请注意: 在我的项目中,Bean 有超过 5000 个属性,其中包含嵌套、复杂和所有类型的对象 豆。真的,我很难找到导致的问题,因为 在一个嵌套对象中具有 StringBuilder 属性,但它是不同的 从上面的例外。所以我不知道其他人是如何找到这些的 的问题,因为异常与实际问题不同。

请求标头是: 标头:{Accept=[application/json, application/*+json], Content-Type=[application/json;charset=UTF-8]。我 在客户端和服务器上都获得相同的请求标头。

请求正文: {"id":"1","name":"Shas","sb":null}

如果有人有解决方案,请分享。

客户端:

final String uri = "http://localhost:8282/WS/rest/add";
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new LoggingRequestInterceptor());
restTemplate.setInterceptors(interceptors);
Person person = new Person();
person.setId("1");
person.setName("Siva");
restTemplate.postForObject(uri, person, Person.class);

服务器端:

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Person addPerson(@RequestBody final Person person) {
        try {
            if (person != null) {
                System.out.println("Person ID : "+person.getId());
            } else {
                System.out.println("Request Obj is null at ws controller");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return person;
}

Person.java

public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id = null;
    private String name = null;
    private StringBuilder sb = null;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public StringBuilder getSb() {
        return sb;
    }
    public void setSb(StringBuilder sb) {
        this.sb = sb;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

异常完整跟踪:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://":Server returned HTTP response code: 415 for URL: http://; nested exception is java.io.IOException: Server returned HTTP response code: 415 for URL: http://
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:580)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:330)
    at com.xxx.xxx(XXX.java:2782)
Caused by: java.io.IOException: Server returned HTTP response code: 415 for URL: http://
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1624)
    at org.springframework.http.client.SimpleClientHttpResponse.getBody(SimpleClientHttpResponse.java:81)
    at org.springframework.http.client.BufferingClientHttpResponseWrapper.getBody(BufferingClientHttpResponseWrapper.java:69)
    at com.xxx.LoggingRequestInterceptor.traceResponse(LoggingRequestInterceptor.java:41)
    at com.xxx.LoggingRequestInterceptor.intercept(LoggingRequestInterceptor.java:22)
    at org.springframework.http.client.InterceptingClientHttpRequest$RequestExecution.execute(InterceptingClientHttpRequest.java:84)
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:69)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:569)
    ... 4 more

【问题讨论】:

  • 客户端是否需要sb,因为你可以忽略它与@JsonIgnore的json对话
  • 当前我正在通过@JsonIgnore 做。但我需要一个解决方案。
  • 你能在你的java机器上附加完整的异常跟踪吗?
  • @Amer 我已添加,请查看。
  • 同时发布您传入的 JSON。该错误似乎表明它可能已损坏(或根本不是 JSON)

标签: java spring rest jackson spring-rest


【解决方案1】:

您能否提供您尝试访问的 API 的 AcceptContent-Type 标头?

【讨论】:

  • @Durlabh,我已经更新了请求标头。请检查一下。
猜你喜欢
  • 2011-05-25
  • 2011-08-03
  • 2011-08-04
  • 1970-01-01
  • 2014-04-06
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
相关资源
最近更新 更多