gpt4 book ai didi

java - Spring REST Jackson - StringBuilder 不与 JSON 绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:52 25 4
gpt4 key购买 nike

我正在使用 Spring MVC RESTful Web 服务,客户端是 RestTemplate。使用 Jackson 将 json 绑定(bind)到 bean 一切正常,但是当我在 bean 中添加 StringBuilder 属性时,客户端会出现异常

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

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

Please note : In my project, Bean have over 5000 properties with nested, complex and all types of objects are having in that single bean. Really i struggled to found that problem causing because of having StringBuilder property in one nested object but it is different from above exception. So i don't know how others are finding these kind of problems since exception is different from actual problem.

Request headers are : Headers : {Accept=[application/json, application/*+json], Content-Type=[application/json;charset=UTF-8]. I am getting same request header on both client as well as server.

Request body : {"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

最佳答案

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

关于java - Spring REST Jackson - StringBuilder 不与 JSON 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43496953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com