Spring MVC: @ResponseBody, 415 Unsupported Media Type
StanMax lead my in the right direction...
First I had to set the @JsonManagedReference
and @JsonBackReference
right. I set the ManagedReference to the attribute which represents the ONE of of the ONE-to-MANY relationship, but apparently you have to set it to the MANY attribute (the collection)
see the description here http://wiki.fasterxml.com/JacksonFeatureBiDirReferences
@JsonBackReference is the "back" part of reference: it will be omitted from serialization, and re-constructed during deserialization of forward reference.
- Annotated property must be of bean type
The problem in my case was that I didn't send sufficient information in my JSON POST to match the Hibernate requirements.
This resulted in an Exception that the proper fields to create a Hibernate object out of my JSON POST, were not present.
I created a test url where I created the Object mapper myself and tried to de-serialize JSON by hand. This throw the right error message.
It seems to my that Spring unfortunately throws the 415 "Unsupported Media Type" exception which is a bit misleading.
So for the future. Try it out by hand first (see example included) and than go on.
ModelMap map = new ModelMap();
logger.info("HibernateAware");
HibernateAwareObjectMapper mapper = new HibernateAwareObjectMapper();
String jsonInput =
"{" +
"\"id\":\"1\"," +
"\"matCountry\":{" +
"\"id\":\"1\"" +
"}" +
"}";
Seizure seizure = mapper.readValue(jsonInput, Seizure.class); // this line throw the exception
Hope this helps someone.
Regards JS
One thing you can try is to figure out if the problem is with Jackson's data binding (fails to serialize hibernate object) or something else. This can be done by manually constructing ObjectMapper, checking to see if 'mapper.serializeValueAsString()' works or throws an exception.
There is a good chance it has to do with Jackson handling of lazily-loaded properties; if so, you may need to use jackson hibernate module as it can handle Hibernate-specific details.
I had the same problem. After reading through how MappingJacksonHttpMessageConverter works I realized that I had not initialized it properly. So I used the following in my rest-servlet.xml and it worked..
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="objectMapper">
<ref bean="JacksonObjectMapper" />
</property>
</bean>
</list>
</property>
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="useNotAcceptableStatusCode" value="true" />
</bean>
<bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />