JSON HttpMediaTypeNotSupportedException
I'm getting an exception when sending JSON over to a Spring Annotated Controller. I believe the problem is related to converting a JSON into a RequestBody. I am unable to switch to full MVC Annotations - I have to use SimpleUrlHandlerMapping for now
Code:
2010-02-15 16:59:53,437 ERROR [ingExceptionResolver][TP-Processor2 ] Error thrown while executing request
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:556)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:283)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
This is the form + javascript modified from mvc-ajax sample
http://blog.springsource.com/2010/01...in-spring-3-0/
Code:
<form:form id="createUser" modelAttribute="user" method="post">
<fieldset>
<legend>User Fields</legend>
<p>
<form:label id="nameLabel" for="name" path="username" cssErrorClass="error">Name</form:label><br/>
<form:input path="username" /><form:errors path="name" />
<form:label id="idLabel" for="id" path="id" cssErrorClass="error">Id</form:label><br/>
<form:input path="id" /><form:errors path="id" />
</p>
<p>
<input id="create" type="submit" value="Create" />
</p>
</fieldset>
</form:form>
<script type="text/javascript">
$(document).ready(function() {
$("#createUser").submit(function() {
var user = $(this).serializeObject();
$.postJSON("user", user, function(data) {
$("#id").val(data.id);
alert("got response");
});
return false;
});
});
</script>
This is a snippet of the server side code:
Code:
@Controller(value="accountsController")
public class AccountController {
....
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> create(@RequestBody User user, HttpServletResponse response) {
return Collections.singletonMap("id", user.getId());
}
}
I had a problem using <mvc:annotation-driven/> on it's own, I read somewhere that I need to declare SimpleControllerHandlerAdapter as well.
Code:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
....
<prop key="/accounts/**/*">accountsController</prop>
....
</props>
</property>
<property name="alwaysUseFullPath" value="true"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean id="formHttpMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean id="bufferedImageHttpMessageConverter" class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</util:list>
</property>
</bean>
Any help would be appreciated
Thanks
Poor jackson error handling
To expand on this even further, you cannot have any configuration errors with your Jackson annotations. In my case, I had multiple @JsonBackReference in a domain object and it was throwing an error (they must have unique names) but trapping it without logging:
org.codehaus.jackson.map.deser.StdDeserializerProv ider
Code:
public boolean hasValueDeserializerFor(DeserializationConfig config, JavaType type)
{
/* Note: mostly copied from findValueDeserializer, except for
* handling of unknown types
*/
JsonDeserializer<Object> deser = _findCachedDeserializer(type);
if (deser == null) {
try {
deser = _createAndCacheValueDeserializer(config, type, null);
} catch (Exception e) {
return false;
}
}
return (deser != null);
}