且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[SpringMVC] 使用@ResponseBody返回JSON数据报错406

更新时间:2022-06-22 02:39:47

问题:
控制器要返回转换成JSON的对象时,使用注解@ResponseBody报错,服务器返回406。
解决办法:

  1. JSON依赖不仅需要jackson-core-asl和jackson-mapper-asl,还需要jackson-databind依赖。
  2. SpringMVC配置时除了<mvc:annotation-driven/>,还需要添加配置。
    控制器 - Java:
package com.spz.spzblog.controller;

import com.spz.spzblog.po.Article;
import com.spz.spzblog.po.Collections;
import com.spz.spzblog.service.IndexService;
import com.spz.spzblog.vo.IndexPageVo;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.List;

//首页第一次打开时加载内容
@Controller
public class IndexController {

    @Autowired
    private IndexService indexServiceImpl;

    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public @ResponseBody IndexPageVo index(Model model)throws Exception{
        IndexPageVo indexPageVo = new IndexPageVo();
        List<Article> articles = indexServiceImpl.indexNewArticles();
        List<Collections> collectionses = indexServiceImpl.indexHotCollections();
        indexPageVo.setNewArticles(articles);
        indexPageVo.setHotCollections(collectionses);
        indexPageVo.setHotUsers(null);
        return indexPageVo;
    }
}

pom.xml:

<!-- JSON 交互 -->
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-core-asl</artifactId>
  <version>1.9.11</version>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.9.13</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.7.1-1</version>
</dependency>

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

    <!-- 自动加载注解处理器和适配器 - 这是原来的配置,删除改为下面的配置 -->
    <!--<mvc:annotation-driven/>-->
    <!-- 修改后的配置 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json; charset=UTF-8</value>
                        <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 配置Handler 组件扫描控制器所在包的所有控制器 -->
    <context:component-scan base-package="com.spz.spzblog.controller"/>

</beans>

前端页面访问 index.action - JavaScript:

//文档树、文件图片加载完成后调用
window.onload = function () {
    //页面首次访问时请求首页数据
    $.ajax({
        type:'post',
        url:'${pageContext.request.contextPath}/index.action',
        contentType:'application/json;charset=utf-8',
        success:function (data) {
            alert(data);
        }
    });
}

参考
http://blog.csdn.net/pc_amoon/article/details/51785461