我正在尝试使用 Spring 创建 Restful 服务。
方法通过参数接受“UserContext”对象,即@RequestBody。
客户端发送内容类型为“application/json”的JSON对象。但是我收到错误“HTTP/1.1 415 不支持的媒体类型”。
..即使客户端发送了一个空的“{}”JSON 对象。
我的 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;
}
}
用户上下文.java
public class UserContext {
private Long userId;
private String userName;
private UserAddress userAddress;
private CustomerInfo customerInfo;
}
应用上下文:
<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/>
纠结了好久。帮助将不胜感激!
最佳答案
根据我在 mvc-showcase 的 messageconverter 示例中看到的内容,尝试在 application/json
请求中使用 Accept
header 。
这是一个相关问题:use spring mvc3 @ResponseBody had 415 Unsupported Media Type why?
关于java - Spring Rest JSON 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5624374/