Spring MVC仅使用ContentNegotiationManagerFactoryBean返回json 406

时间:2014-12-27 17:15:26

标签: java spring-mvc http-accept-header

我希望只针对servlet上下文作为“home”的http://localhost:8090/home/home/home2的任何请求启用JSON响应。

我设置的配置是,如果没有接受标头,则返回默认值为“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;
    }

}

1 个答案:

答案 0 :(得分:0)

如果您使用@ResponseBody或@RequestBody,则意味着默认情况下将激活Spring Message Conversions机制,并且您的ContentNegotiationManager无法获得控制权。

但是为什么MappingJackson2HttpMessageConverter(或以前的版本MappingJacksonHttpMessageConverter)不起作用?我想你忘记添加jackson库了:

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