2016-05-14 79 views
0

我從第3版(基於XML)升級春季4.2.4(基於註解的)錯誤415 - 同春不支持的媒體類型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客戶端。我也試過路過的URL 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 

可有人請建議如何獲得PUTPOST運營工作?

回答

0

如果您爲控制器方法指定produces,則還需要在請求上設置Accepts標頭。

+0

我試過只消耗也。同樣的問題仍然存在。控制永遠不會更新方法。 –

+0

嘗試在您的網址中添加'?format = json'。如果那是有效的,那麼你的內容談判經理正在搞東西。人們通常在使用談判管理器的註釋中指定消費/生產,而不是兩者 –

相關問題