I\'m trying to insert and/or update data into the database using the PUT
method via JSON using jQuery 1.6, (Jackson 2.1.1 and Spring 3.
In the server side, in Spring 3, you need this:
<bean id = "mappingJacksonHttpMessageConverter" class = "org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- starting Spring MVC annotation usage,handling request and annotation pojo mapping-->
<bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name= "messageConverters" >
<list>
<ref bean= "mappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
Spring 4 uses mappingJackson2HttpMessageConverter
.
Without AnnotationMethodHandlerAdapter
bean declaration, you can also use @RequestBody
, while by declaring it we can set mappingJackson2HttpMessageConverter
to messageConverters
. This is concluded by phenomena that I observed, if wrong please correct.
Just in case somebody encounters this: I had two Methods
void setLevel (Level level) void setLevel (String level)
in the Class annotated with @RequestBody and that also caused a 415.
The content type that the browser sends, Content-Type: application/json
, does not seem to match @RequestBody final MultiValueMap<String, String > data
.
Either:
Send application/x-www-form-urlencoded
(in which case you could easier use HttpPutFormContentFilter instead).
Or: change this into something like @RequestBody final MyDTO data
as explained in JQuery, Spring MVC @RequestBody and JSON - making it work together.
I think your browser is not supporting whatever your returning. An explaination of an HTTP 415 seems to indicate this. What is the server sending as the Content-Type in the response?
I had the same problem, the way to solve it was removing @RequestBody and then taking the data from the request (with IOUtils from apache commons). It should be noted that I used the received data just for logging js errors.
/**
* this method logs with log4j the received js errors
*/
@RequestMapping(value = "/jsloggerservice/{applicationName}", method = RequestMethod.POST)
public void jsLogger(HttpServletRequest request, HttpServletResponse response) {
try {
String message = IOUtils.toString( request.getInputStream());
log.info("JAVASCRIPT-ERROR: " + message);
response.getWriter().write("OK");
}
catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}