我正在将Spring从版本3(基于XML)升级到4.2.4(基于注释)
控制器
@RestController
public class XnetOrderResourceController{
@Autowired
private XnetOrderDao orderDao;
/*
* GET All Orders
*/
@RequestMapping(value = "/getAllOrdersInfo", method =RequestMethod.GET, produces="application/json")
public XnetOrder get(@RequestParam("Id") Long Id) {
XnetOrder order=null;
try{
if(MiscUtils.isNotEmpty(Id)){
order=orderDao.get(Id);
}
}catch(HibernateException e){
log.info(ORDER_DETAIL_NOT_AVAILABLE);
getLogger().error(e.getMessage());
}
return order;
}
/*
* POST(create new) Orders
*/
@RequestMapping(value = "/insertOrders", method =RequestMethod.POST, consumes="application/json", produces="application/json")
public void post(@RequestBody XnetOrder order){
try{
orderDao.post(order);
}catch(HibernateException e){
log.info("Creating the Order operation failed.");
getLogger().error(e.getMessage());
}
}
/*
* PUT (update) orders
*/
@RequestMapping(value="/updateOrders", method=RequestMethod.PUT, consumes="application/json", produces="application/json")
public XnetOrder update(@RequestBody XnetOrder order){
try{
order=orderDao.put(order);
}catch(HibernateException e){
getLogger().error(e.getMessage());
}
return order;
}
/*
* DELETE orders
*/
@RequestMapping(value="/deleteOrders",method=RequestMethod.DELETE)
public void delete(@RequestParam("Id") Long Id){
try{
if(MiscUtils.isNotEmpty(Id)){
orderDao.delete(Id);
log.info("Order is successfully deleted");
}
}catch(HibernateException e){
log.info(XNET_PRODUCT_INVALID_ERROR_MESSAGE);
getLogger().error(e.getMessage());
}
}
}
弹簧servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.nest.extranet" />
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="mediaTypes">
<value>
json=application/json
</value>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我的类路径中的JAR文件是 -
jackson-annotations-2.2.3.jar,
jackson-core-2.2.3.jar,
jackson-databind-2.3.3.jar,
jackson-mapper-asl-1.9.13.jar,
jackson-2.1.0-all.jar
我能够执行GET
操作,该操作成功返回JSON值,但PUT
和POST
操作无法正常工作。我使用Postman作为REST客户端。我还尝试在将标头值PUT
设置为Content-Type
之后传递aplication/json
操作的网址,但它始终返回
错误415 - 不支持的媒体类型。
我收到以下回复标题:
Connection → close
Content-Length → 903
Content-Type → text/html; charset=UTF-8
Date → Sat, 14 May 2016 09:16:57 GMT
X-Powered-By → Servlet/2.5 JSP/2.1
有人可以建议如何让PUT
和POST
操作起作用吗?
答案 0 :(得分:0)
如果为控制器方法指定produces
,则还需要在请求中设置Accepts
标题。