我试图用Spring创建一个Restful服务。Spring Rest JSON绑定
一个方法通过参数接受一个“UserContext”对象,即@RequestBody。
客户端发送内容类型为“application/json”的JSON对象。但是我收到错误“HTTP/1.1 415 Unsupported Media Type”。
......即使客户端发送空的“{}”JSON对象。
我的控制器:
@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;
}
应用程序上下文:
<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/>
与此挣扎了一会儿。帮助将不胜感激!
我有这些作为Maven项目依赖.. – Sri 2011-04-12 07:21:07