Java – spring rest JSON binding
•
Java
I'm trying to create restful services with spring
Method accepts the "usercontext" object through parameters, that is, @ requestbody
The client sends JSON objects using the content type "application / JSON" But I received the error "http / 1.1 415 unsupported media type"
.. Even when the client sends an empty "{}" JSON object
My controller:
@Controller @RequestMapping(value = "/entityService") class RestfulEntityService { @Resource private EntityService entityService; @ResponseBody @RequestMapping(value = "/getListOfEntities",method = RequestMethod.POST) public List<Entity> getListOfEntities(@RequestBody UserContext userContext) { System.out.println(userContext); return null; } }
UserContext. java
public class UserContext { private Long userId; private String userName; private UserAddress userAddress; private CustomerInfo customerInfo; }
Application background:
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"/> <bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> <constructor-arg ref="xstreamMarshaller"/> <property name="supportedMediaTypes" value="application/xml"/> </bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list id="beanList"> <ref bean="xmlMessageConverter" /> <ref bean="jsonHttpMessageConverter"/> </util:list> </property> </bean> <mvc:annotation-driven/>
He struggled for a while Help will be appreciated!
Solution
Based on what I saw in the messageconverter example of MVC showcase, try using the accept header in the application / JSON request
This is a related question: use spring mvc3 @ ResponseBody had 415 unsupported media type why?
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码