Spring REST Jackson - StringBuilder与JSON没有绑定

时间:2017-04-19 13:19:11

标签: java spring rest jackson spring-rest

我正在使用Spring MVC RESTful webservices&客户端是RestTemplate。通过使用jackson将json绑定到bean,一切正常,但是当我在bean中添加StringBuilder属性然后客户端获得异​​常

org.springframework.web.client.ResourceAccessException:POST请求的I / O错误" http://":服务器返回HTTP响应代码:415为URL: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":空}

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

客户端:

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

1 个答案:

答案 0 :(得分:0)

您是否可以提供您尝试访问的API的AcceptContent-Type标题?