先参考文章:【Spring MVC】使用IDEA创建Spring MVC的Maven项目 ,创建 Spring MVC 项目。
一、配置Spring
dispatcher-servlet.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: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.0.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 "> <!-- 配置注解驱动,可以把request参数绑定到controller参数上 --> <mvc:annotation-driven/> <context:component-scan base-package="com.wxnacy.spring.controller"/> </beans>
HttpMessageConverter 接口需要在配置中开启 <mvc:annotation-driven/>
,并且需要在文件的 beans 定义中需要声明 mvc 的 xmlns:mvc
和 xsi:schemaLocation
不然就会报错。
二、配置JSON库
Spring 默认是由 Jackson 来解析 JSON 的,所以需要在 pom.xml 中引入对应包。
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <!-- Json 解析库 --> <!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> <!-- 注意:如没有用到注解去配置,可不用引入此依赖 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.12.3</version> </dependency>
三、新建控制器,测试一下
package com.wenjianbao.controller; import com.wenjianbao.entity.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.List; /** * 示例 */ @Controller @RequestMapping("/demo") public class DemoController { @GetMapping("/json") @ResponseBody public Object json() { List<User> userList = new ArrayList<>(); User user1 = new User(); user1.setId(1); user1.setLoginName("wenjianbao"); user1.setUserName("温建宝"); User user2 = new User(); user2.setId(2); user2.setLoginName("zhangsan"); user2.setUserName("张三"); User user3 = new User(); user3.setId(3); user3.setLoginName("lisi"); user3.setUserName("李四"); userList.add(user1); userList.add(user2); userList.add(user3); return userList; } }
输出:
[ { "id": 1, "loginName": "wenjianbao", "userName": "温建宝" }, { "id": 2, "loginName": "zhangsan", "userName": "张三" }, { "id": 3, "loginName": "lisi", "userName": "李四" } ]
四、也可以使用其他JSON库,如:Fastjson
Fastjson 是号称解析速度最快的 JSON 转换工具,由 Alibaba 团队开发完成的。
使用也很简单,先引入包
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.44</version> </dependency>
dispatcher-servlet.xml增加一些配置:
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json;charset=UTF-8"/> <property name="features"> <array> <!-- [>QuoteFieldNames———-输出key时是否使用双引号,默认为true<] --> <!-- [>WriteMapNullValue——–是否输出值为null的字段,默认为false<] --> <!-- [>WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null<] --> <!-- [>WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null<] --> <!-- [>WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null<] --> <!-- [>WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null<] --> <!-- [>DisableCircularReferenceDetect 关闭循环引用<] --> <value>WriteNullNumberAsZero</value> <value>WriteNullListAsEmpty</value> <value>WriteNullStringAsEmpty</value> <value>WriteNullBooleanAsFalse</value> <value>DisableCircularReferenceDetect</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
就这么简单,再次启动项目,即可看到效果。
参考:
Java操作JSON的三种库对比 Jackson、Gson、Fastjson