Fork me on GitHub

配置JSON视图406问题

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

http://stackoverflow.com/questions/3340050/springs-json-not-being-resolved-with-appropriate-response
http://stackoverflow.com/questions/7462202/spring-json-request-getting-406-not-acceptable?lq=1
http://stackoverflow.com/questions/4856298/spring-mvc-3-returning-xml-through-responsebody

  <!-- 配置JSON视图解析-->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>
 
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>
 
    <!-- 或者配置JSON视图解析-->
 
    <bean id="jsonView"
        class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        <property name="encoding">
            <value type="org.codehaus.jackson.JsonEncoding">UTF8</value>
        </property>
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>    
    <mvc:annotation-driven />

或者如官方文档所述

In Spring 3, you can enable “mvc:annotation-driven” to support object conversion to/from JSON format, if Jackson JSON processor is existed on the project classpath.

Add “@ResponseBody” in the return value, no much detail in the Spring documentation.

As i know, when Spring see

  1. Jackson library existed on classpath
  2. “mvc:annotation-driven” is enabled
  3. Return method annotated with @ResponseBody

It will handle the JSON conversion automatically.

Enable “mvc:annotation-driven” in your Spring configuration XML file

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.mkyong.common.controller" />
    <mvc:annotation-driven />
 
</beans>

 

如下面Code

@Controller
@RequestMapping("/json")
public class JsonController {
    @RequestMapping("/view.shtml")
    public @ResponseBody
    User view() {
        User user = new User();
        user.setEmail("Irving_Zhou@outlook.com");
        user.setNickname("irving");
        user.setAdddate(new Date());
        return user;
    }

    @RequestMapping("/map.shtml")
    public @ResponseBody
    Map<String, Object> map() {
        User user = new User();
        user.setEmail("Irving_Zhou@outlook.com");
        user.setNickname("irving");
        user.setAdddate(new Date());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user", user);
        map.put("totle", 50);
        return map;
    }

    @RequestMapping("/list.shtml")
    public @ResponseBody
    List<User> list() {
        List<User> list = new ArrayList<User>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setEmail("Irving_Zhou@outlook.com");
            user.setNickname("irving");
            user.setAdddate(new Date());
            list.add(user);
        }
        return list;
    }

    @RequestMapping("/listtest.shtml")
    public @ResponseBody
    List<User> listtest() {
        List<User> list = new ArrayList<User>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setEmail("Irving_Zhou@outlook.com");
            user.setNickname("irving");
            user.setAdddate(new Date());
            user.setTruename("哈哈哈");
            list.add(user);
        }
        return list;
    }
}

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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <!-- 注解驱动-->
    <mvc:annotation-driven />

    <!-- 扫描注解类 -->
    <context:component-scan base-package="cn.base.web.controller" />
    <context:component-scan base-package="cn.base.web.dao.impl" />
    <context:component-scan base-package="cn.base.web.service.impl" />
</beans>

Just Test:

image

@ResponseBody乱码的解决方案

<!--启动Spring MVC的注解功能,完成请求和注解POJO的映射并且设置ResponseBody返回编码 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
    <property name="messageConverters">   
         <list>   
             <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> 
                <constructor-arg>
                    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                        <property name="targetClass" value="java.nio.charset.Charset" />
                       <property name="targetMethod" value="forName"/>
                       <property name="arguments" value="UTF-8"/>
                    </bean>
                </constructor-arg>
            </bean>   
         </list>   
   </property>  
</bean>
posted @ 2013-04-20 15:42  花儿笑弯了腰  阅读(807)  评论(0编辑  收藏  举报