示例 自定义HttpMessageConverter返回JSON格式的数据
接下来,使用Fastjson
来返回JSON
数据。
创建一个Fastjson2Test
项目,在WebContent
目录下创建一个js
目录,加入jQuery
和json2
的js
文件,在WEB-INF/lib
目录中加入Fastjson
的jar
文件。
Fastjson2Test
项目的所有JSP
和Java
文件和ResponseBodyTest
一致,只是在springmvc-config.xml
中使用了Fastjson
的FastJsonHttpMessageConverter
。读者可参考配套资源文件中对应的代码,测试结果和ResponseBodyTest
项目一致,此处不再赘述.
项目结构
展开/折叠
G:\Desktop\随书源码\Spring+Mybatis企业应用实战(第2版)\codes\03\Fastjson2Test
├─src\
│ └─org\
│ └─fkit\
│ ├─controller\
│ │ └─BookController.java
│ └─domain\
│ └─Book.java
└─WebContent\
├─index.jsp
├─js\
│ ├─jquery-1.11.0.min.js
│ ├─jquery-migrate-1.2.1.min.js
│ └─json2.js
├─META-INF\
│ └─MANIFEST.MF
└─WEB-INF\
├─lib\
│ ├─commons-logging-1.2.jar
│ ├─fastjson-1.2.9.jar
│ ├─spring-aop-5.0.1.RELEASE.jar
│ ├─spring-aspects-5.0.1.RELEASE.jar
│ ├─spring-beans-5.0.1.RELEASE.jar
│ ├─spring-context-5.0.1.RELEASE.jar
│ ├─spring-context-indexer-5.0.1.RELEASE.jar
│ ├─spring-context-support-5.0.1.RELEASE.jar
│ ├─spring-core-5.0.1.RELEASE.jar
│ ├─spring-expression-5.0.1.RELEASE.jar
│ ├─spring-instrument-5.0.1.RELEASE.jar
│ ├─spring-jcl-5.0.1.RELEASE.jar
│ ├─spring-jdbc-5.0.1.RELEASE.jar
│ ├─spring-jms-5.0.1.RELEASE.jar
│ ├─spring-messaging-5.0.1.RELEASE.jar
│ ├─spring-orm-5.0.1.RELEASE.jar
│ ├─spring-oxm-5.0.1.RELEASE.jar
│ ├─spring-test-5.0.1.RELEASE.jar
│ ├─spring-tx-5.0.1.RELEASE.jar
│ ├─spring-web-5.0.1.RELEASE.jar
│ ├─spring-webflux-5.0.1.RELEASE.jar
│ ├─spring-webmvc-5.0.1.RELEASE.jar
│ └─spring-websocket-5.0.1.RELEASE.jar
├─springmvc-config.xml
└─web.xml
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试返回JSON格式的数据</title> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> $(document).ready(function() { testResponseBody(); }); function testResponseBody() { $.post("${pageContext.request.contextPath}/json/testRequestBody", null, function(data) { $.each(data, function() { var tr = $("<tr align='center'/>"); $("<td/>").html(this.id).appendTo(tr); $("<td/>").html(this.name).appendTo(tr); $("<td/>").html(this.author).appendTo(tr); $("#booktable").append(tr); }) }, "json"); } </script> </head> <body> <table id="booktable" border="1" style="border-collapse: collapse;"> <tr align="center"> <th>编号</th> <th>书名</th> <th>作者</th> </tr> </table> </body> </html>
|
BookController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package org.fkit.controller;
import java.util.ArrayList; import java.util.List; import org.fkit.domain.Book; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;
@Controller @RequestMapping( "/json" ) public class BookController { @RequestMapping( value = "/testRequestBody" ) @ResponseBody public Object getJson() { List<Book> list = new ArrayList<Book>(); list.add(new Book(1, "Spring+MyBatis企业应用实战", "肖文吉")); list.add(new Book(2, "轻量级JavaEE企业应用实战", "李刚")); return list; } }
|
springmvc-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <context:component-scan base-package="org.fkit.controller" /> <mvc:default-servlet-handler /> <mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" /> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/content/" p:suffix=".jsp" />
</beans>
|