I am upgrading Spring from version 3 (XML based) to 4.2.4 (annotation based)
Controller
@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());
}
}
}
Spring-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 files in my classpath are -
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
I am able to perform GET
operation, which successfully returns JSON value, but PUT
and POST
operations don't work. I am using Postman as a REST client. I also tried passing the URL for PUT
operation after setting the header value Content-Type
to aplication/json
but it always returns
Error 415 - Unsupported Media Type.
I am getting the following response headers:
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
Can someone please suggest how to get PUT
and POST
operations to work?
如果为控制器方法指定了produces
,则还需要在请求上设置Accepts
标头。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.