关闭

springmvc通过@ResponseBody 自动解析为json对象支持中文

标签: spring mvcmavenjson博客
35人阅读 评论(0) 收藏 举报
分类:

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"}]
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:38次
    • 积分:10
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档