I am using Spring MVC RESTful webservices & Client is RestTemplate. Everything is working fine by using jackson to bind json to bean but when i add StringBuilder property in bean then client getting below exception
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://
Caused by: java.io.IOException: Server returned HTTP response code: 415 for 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}
If anybody have solution then please share it.
Client Side:
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);
Server Side :
@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;
}
}
Exception full trace :
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
on the client, because you can ignore its conversation to json with@JsonIgnore
– Arif Ulusoy