1. 程式人生 > >springmvc通過@ResponseBody 自動解析為json物件支援中文

springmvc通過@ResponseBody 自動解析為json物件支援中文

springmvc通過@ResponseBody 可以自動解析為json物件。今天弄了一下午才弄出來,寫部落格是為了給自己加深印象。

先上maven依賴

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

controller中程式碼

package com.fy
.spring.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype
.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.fy.spring.modol.Person; import com.fy.spring.service.PersonService; @Controller @RequestMapping("/") public class UsersController { @Autowired @Qualifier(value="personService") PersonService ps; @RequestMapping(value="/showperson" ,produces="application/json;charset=utf-8") @ResponseBody public List<Person> ShowPerson(HttpServletRequest req,HttpServletResponse response) { List<Person> list=ps.listPerson(); return list;//在這裡直接返回一個list 會自動解析為json } }

web.xml中的配置檔案部分

 <!-- 解決@ResponseBody註解直接返回物件並轉換成JSON時出現406問題,同時解決了返回String型別亂碼的問題 名稱空間必須在3.2及以上 不寫為預設最新-->
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <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>
前臺用的是easyui接收這裡就不貼程式碼了
前臺顯示程式碼
[{"id":1,"name":"是的傳送到","country":"fdgdfg所發生的"},{"id":3,"name":"3噓","country":"123@qq.x"}]