天天看點

[SpringMVC] 使用@ResponseBody傳回JSON資料報錯406

問題:

控制器要傳回轉換成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