2014-12-27 84 views
1

我只希望將JSON響應轉換爲對http://localhost:8090/home/home/home2爲servlet上下文爲「home」的任何請求。Spring MVC返回json只有406和ContentNegotiationManagerFactoryBean

我設置的配置是,如果沒有接受標頭,將默認返回爲「JSON」響應。 我也嘗試將接受類型設置爲「application/json」,這不起作用。

Spring MVC + JSON = 406 Not Acceptable上的解決方案是使用不同的Messageconverter方法,而我嘗試通過ContentNegotiationManagerFactoryBean類。

的web.xml中是如下

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

該servlet-context.xml中如下。

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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:component-scan base-package="com.andy.main" /> 


    <resources mapping="/resources/**" location="/resources/" /> 



    <beans:bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
     <beans:property name="ignoreAcceptHeader" value="true"/> 
     <beans:property name="defaultContentType" value="application/json" /> 

     <beans:property name="mediaTypes"> 
      <beans:map> 
       <beans:entry key="json" value="application/json" /> 
       <beans:entry key="xml" value="application/xml" /> 
     </beans:map> 
    </beans:property> 
    </beans:bean> 

    <annotation-driven content-negotiation-manager="contentNegotiationManager" /> 


</beans:beans> 

Java代碼如下:

package com.andy.main; 

import java.util.HashMap; 
import java.util.Locale; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

/** 
* Handles requests for the application home page. 
*/ 
@Controller 
@RequestMapping("/home") 
public class HomeController { 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    /** 
    * Simply selects the home view to render by returning its name. 
    */ 
    @RequestMapping(value = "/home2" , method = RequestMethod.GET) 
    public @ResponseBody HashMap<String, String> home(Locale locale, Model model) { 
     System.out.println("In Home2"); 
     logger.info("Welcome home! The client locale is {}.", locale); 
     HashMap<String, String> allValues=new HashMap<String, String>(); 
     allValues.put("Doctor","Dr.John"); 
     allValues.put("Location", "location1"); 
     return allValues; 
    } 

} 

回答

0

如果使用或@ResponseBody @RequestBody就意味着春天Message Conversions機制會被默認啓動,並且您ContentNegotiationManager不得到控制。

但爲什麼MappingJackson2HttpMessageConverter(或以前的版本MappingJacksonHttpMessageConverter)不起作用?我想你忘了添加傑克遜庫:

<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.4.4</version> 
</dependency> 
+0

謝謝。傑克遜庫存是我錯過的。所以,當我打電話給以下網址http:// localhost:8090/home/home/home2.json時,返回給我一個「JSON」。有沒有辦法只爲「http:// localhost:8090/home/home/home2」返回「JSON」 – andyPaul 2014-12-27 20:01:54

+0

是的,如果想要使用MessageConverters,你必須添加下一個頭:Accept = application/json。或刪除@ResponseBody註釋並正確配置您的ContentNegotitationManager。 – 2014-12-28 19:04:44

相關問題