繁体   English   中英

错误415-Spring 4不支持的媒体类型

[英]Error 415 - Unsupported Media Type with Spring 4

我正在将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());
        }
    }
}

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文件是-

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值,但PUTPOST操作不起作用。 我使用Postman作为REST客户端。 在将标头值Content-Typeaplication/json后,我也尝试传递用于PUT操作的URL,但它始终返回

错误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

有人可以建议如何使PUTPOST操作PUT吗?

如果为控制器方法指定了produces ,则还需要在请求上设置Accepts标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM