Spring MVC: @ResponseBody, 415 Unsupported media type

I'm having trouble displaying a JSON Post message on a specific Java object to save it using Hibernate

Ajax call headers set correctly ...

Accept application/json Content-Type application/json; charset=UTF-8 

and the HTTP POST method

Here is my configuration ...

My Spring MVC function mapping is as follows:

 @RequestMapping(value = {"/save.json"},method = RequestMethod.POST) public ModelMap save(@RequestBody Seizure seizureObj,Model model) { ... } 

in my xml container I have a ContentNegotiationViewResolver like this

 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="json" value="application/json" /> </map> </property> <property name="viewResolvers"> <list> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/assets/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"> <property name="prefixJson" value="false" /> <property name="objectMapper" ref="jacksonObjectMapper" /> </bean> </list> </property> </bean> 

in my container xml part for jackson

 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterConfigurer" init-method="init"> <property name="messageConverters"> <list> <bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > <property name="objectMapper" ref="jacksonObjectMapper" /> </bean> </list> </property> </bean> <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" /> 

I have

  • JACKSON-core-ASL-1.8.5
  • JACKSON-Mapper-ASL-1.8.5

in the / lib folder

and Jackson works for a simple case like this

 public class Simple { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } } @RequestMapping(value = {"/saveSimple.json"},method = RequestMethod.POST) public ModelMap save(@RequestBody Simple simple,Model model) { ... } 

when i test it with curl

  curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":1}' http://<URL>/saveSimple.json 

No problem, but if I test it with a Hibernate object, I get a message

 415 Unsupported Media Type 

any ideas.

+6
source share
3 answers

StanMax will lead me in the right direction ...

First, I needed to set the correct @JsonManagedReference and @JsonBackReference . I set the ManagedReference to an attribute that represents the ONE of the ONE-to-MANY relationship, but apparently you need to set it to the MANY attribute (collection)

see description here http://wiki.fasterxml.com/JacksonFeatureBiDirReferences

@JsonBackReference is the "back" part of the link: it will be omitted from serialization and reconstructed during deserialization of the direct link.

  • Annotated property must be bean type

The problem in my case was that I did not send enough information to my JSON POST to meet hibernation requirements.

This led to the exception that the correct fields for creating the Hibernate object from my JSON POST were not present.

I created a test URL where I created the Mapper object myself, and tried to de-serialize JSON manually. This will result in the correct error message.

It seems to me that Spring, unfortunately, throws an exception 415 "Unsupported Media Type", which is a little misleading.

So for the future. First try manually (see Example) and continue.

  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

+13
source

One thing you can try is to find out if the problem is with Jackson's data binding (failed to serialize the sleep object) or something else. This can be done by manually creating an ObjectMapper, checking if "mapper.serializeValueAsString ()" is working or throwing an exception.

There is a good chance this is due to Jackson using lazily loaded properties; If so, you may need to use the hibernate jackson module , as it can handle Hibernate related details.

+4
source

I had the same problem. After reading how MappingJacksonHttpMessageConverter works, I realized that I did not initialize 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" /> 
+2
source

Source: https://habr.com/ru/post/895615/


All Articles