示例 自定义HttpMessageConverter 接收JSON格式的数据 Spring默认使用Jackson处理JSON数据。在实际开发中,开发者也可以选择使用其他开源框架处理JSON数据。那么,如果使用其他的开源框架处理JSON数据,该如何配置HttpMessageConverter呢?接下来,我们就使用在业界非常受欢迎的Fastjson来接收JSON数据。
下载Fastjson
本书成书时Fastjson开源框架的最新版本是1.2.9。jar包只有1个:fastjson-1.2.9.jar。建议读者进入该地址 下载该版本或者更高版本进行测试。
项目示例 创建一个FastjsonTest项目,在WebContent目录下创建一个js目录,加入jQuery和json2的js文件,在WEB-INF/lib目录中加入Fastjson的jar文件。
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 package org.fkit.controller;import javax.servlet.http.HttpServletResponse;import org.fkit.domain.Book;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import com.alibaba.fastjson.JSONObject;@Controller @RequestMapping ("/json" )public class BookController { @RequestMapping (value = "/testRequestBody" ) public void setJson (@RequestBody Book book, HttpServletResponse response) throws Exception { System.out.println(JSONObject.toJSONString(book)); book.setAuthor("作者名字" ); response.setContentType("text/html;charset=UTF-8" ); response.getWriter().println(JSONObject.toJSONString(book)); } }
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 54 55 56 57 <?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:p ="http://www.springframework.org/schema/p" xmlns:mvc ="http://www.springframework.org/schema/mvc" xmlns:util ="http://www.springframework.org/schema/util" xmlns:context ="http://www.springframework.org/schema/context" xsi:schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.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 >
以上配置文件和之前的配置文件主要的区别在于,之前使用的是Spring中默认的MappingJackson2HttpMessageConverter,这样只需要配置默认的<mvc:annotation-driven/>就可以了。而现在使用第三方的开源框架Fastjson处理JSON数据,则需要另行配置HttpMessageConverter。Spring MVC默认使用MappingJackson2JsonView转换器,所以必须加入Jackson这个库的第三方类文件。而在实际开发中,更加受欢迎的是Fastjson,所以本例并没有使用Jackson,而是使用了Fastjson,则转换器需要配置成com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter类型,该类是Fastjson中实现了HttpMessageConverter接口的类。 如果加入了Fastjson相关jar文件,但是没有配置FastJsonHttpMessageConverter转换器,则在发送请求时后台会提示错误:
1 Handler execution resulted in exception: Content type application/json;charset=UTF-8 not supported
此外,其他JSP和Java文件和之前项目的一致,并且还需要在web.xml文件中配置Spring MVC的前端控制器DispatcherServlet,因为每次配置基本一致,此处不再赘述读者可自行配置. 部署FastjsonTest这个Web应用,在浏览器中输入如下URL来测试应用:
1 http://localhost:8080/FastjsonTest/
浏览器显示效果:
由此可知,处理JSON格式的开源框架使用Jackson和Fastjson,只是需要使用不同的HttpMessageConverter而已.
原文链接: 示例 自定义HttpMessageConverter 接收JSON格式的数据