SpringMVC整合fastjson-1.1.41
以前用fastjson也只是硬编码,就好像这篇博文写的http://blog.csdn.net/jadyer/article/details/24395015
昨天心血来潮突然想和SpringMVC整合,然后利用@ResponseBody注解的方式序列化输出json字符串
下面是研究成果
首先是applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.jadyer"/> <mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
<property name="features">
<array>
<value>WriteMapNullValue</value>
<value>WriteNullStringAsEmpty</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <!-- fastjson-1.1.41与SpringMVC整合 -->
<!--
1)若按照jackson和SpringMVC的整合方式,应按照下面的写法,但测试发现这样会报告"HTTP Status 406"
The resource identified by this request is only capable of generating responses
with characteristics not acceptable according to the request "accept" headers.
2)测试通过的整合方式为上面那样在mvc:annotation-driven里面进行注册
3)supportedMediaTypes增加[text/html;charset=UTF-8]值,是为了兼容IE6
否则[application/json]值在IE6中会导致弹出对话框询问是否保存文件,而firefox等高级浏览器会正常打印json字符串
4)若像下面这样给supportedMediaTypes属性赋两个值[text/html;charset=UTF-8]和[application/json],则[application/json]是无效的
因为此时应答给浏览器(或者说请求方)的Content-Type头信息都是[text/html;charset=UTF-8],所以给它一个值就行了
如果给supportedMediaTypes的值为[application/json],则应答给浏览器的Content-Type头信息就是[application/json;charset=UTF-8]
5)关于features属性,不是serializerFeature,而是features,详见FastJsonHttpMessageConverter.java
它是用来控制json序列化输出时的一些额外属性,比如说该字段是否输出、输出时key使用单引号还是双引号、key不使用任何引号等等
QuoteFieldNames----------输出key时是否使用双引号,默认为true
WriteMapNullValue--------是否输出值为null的字段,默认为false
WriteNullNumberAsZero----数值字段如果为null,输出为0,而非null
WriteNullListAsEmpty-----List字段如果为null,输出为[],而非null
WriteNullStringAsEmpty---字符类型字段如果为null,输出为"",而非null
WriteNullBooleanAsFalse--Boolean字段如果为null,输出为false,而非null
6)通常在网上搜到的fastjson和springMVC整合的例子中都像下面注释的代码那样给了两个属性WriteMapNullValue和QuoteFieldNames
这就表示为json解析器设置QuoteFieldNames和WriteMapNullValue的值为true,即输出时key使用双引号,同时也输出值为null的字段
7)输出时某字段为String类型,且值为null,此时若需要其输出,且输出值为空字符串,则需同时赋值WriteMapNullValue和WriteNullStringAsEmpty
经测试,若只赋值WriteNullStringAsEmpty,则不会输出该字段..加上WriteMapNullValue属性后,便输出了,且输出值不是null,而是预期的空字符串
-->
<!--
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="serializerFeature">
<array>
<value>QuoteFieldNames</value>
<value>WriteMapNullValue</value>
</array>
</property>
</bean>
</list>
</property>
</bean>
-->
<!--
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</list>
</property>
</bean>
-->
</beans>
接着是SpringMVC中的Controller
package com.jadyer.controller; import java.io.IOException;
import java.io.PrintWriter; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON;
import com.jadyer.model.LoginResult; @Controller
@RequestMapping(value="/account")
public class AccountController {
@Resource
private AccountService accountService; // @RequestMapping(value="/login")
// public String login(String username, String password, HttpServletResponse response) throws IOException{
// response.setContentType("text/plain; charset=UTF-8");
// response.setHeader("Cache-Control", "no-cache");
// response.setHeader("Pragma", "no-cache");
// response.setDateHeader("Expires", 0);
// PrintWriter out = response.getWriter();
// out.write(JSON.toJSONString(accountService.login(username, password)));
// out.flush();
// out.close();
// return null;
// }
@ResponseBody
@RequestMapping(value="/login")
public LoginResult login(String username, String password){
LoginResult result = new LoginResult();
//验签过程暂略....
result = accountService.login(username, password);
return result;
}
}
最后是login()方法的应答实体类LoginResult.java
package com.jadyer.model; public class LoginResult {
private String respCode; //应答码
private String respDesc; //应答描述
private String userId; //用户编号
private String username; //用户名
private String mobileNo; //用户手机号
private String integral; //用户拥有的积分
/*-- 对应的setter和getter略 --*/
}
SpringMVC整合fastjson-1.1.41的更多相关文章
- springmvc整合fastjson
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SpringMVC整合fastjson、easyui 乱码问题
一.框架版本 SpringMVC:3.1.1.RELEASE fastjson:1.2.7 easyui :1.4.5 二.乱码现象 Action中使用@ResponseBody返回Json数据 ...
- SpringMVC整合FastJson:用"最快的json转换工具"替换SpringMVC的默认json转换
2017年11月23日 09:18:03 阅读数:306 一.环境说明 Windows 10 1709 Spring 4.3.12.RELEASE FastJson 1.2.40 IDEA 2017. ...
- Springmvc整合tiles框架简单入门示例(maven)
Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(略有修改)
对应项目的代码地址为:https://github.com/liuxiaoming7708/springboot-dubbo-parent Dubbo与Zookeeper.SpringMVC整合和使用 ...
- SpringBoot2.0整合fastjson的正确姿势
SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将J ...
- (转)Dubbo与Zookeeper、SpringMVC整合和使用
原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...
- SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
随机推荐
- Java枚举类型理解
Enum格式理解 Enum的格式可以看做跟class关键字一样 class的定义格式如下: public class abc{ } enum的定义格式如下: Public enum abc { } 引 ...
- How JSP work.
A JSP page exists in three forms: JSP source code: consists of a mix of HTML template code. Java lan ...
- Css简介
- Linux命令行下svn ignore忽略文件或文件夹用法
一.忽略单个目录 1.忽略文件夹 假如目录oa.youxi.com是从svn checkout出来的,在服务器本地目录添加了material,但是不希望把material加入版本控制,因此我们需要忽略 ...
- C#简单一句代码,实现pictureBox的照片另存为磁盘文件不出错
开发人事档案系统时,一般都要利用pictureBox对人员的照片进行操作,包括选择保存照片.另存照片.删除照片,如下图: 将照片保存到数据库和从数据库中删除,网友写了很多实用代码,非常好用.但是要将p ...
- html5 的百度地图连接
在一些网站上,我们经常会看到一些地址会有一个图标的形式展现,当你点击的时候就会加载一个你点击区域的地图出来,很神奇的一个功能,在之前是没有这样功能的,都是直接写上地址,你要去的话自己找去吧,现在有了这 ...
- 【POJ1753】Flip Game
[题目大意] 有一个4x4规格的一个棋盘,现在有16个一面黑一面白的棋子分布在这个棋盘上. 翻转一个棋子能够使它以及它上下左右的四个棋子从黑变白,从白变黑. 现在问你至少要经过多少次操作才能够使得整个 ...
- backbone学习笔记(一)
因为工作的需要,从今天起对backbone的学习过程做下记录. 学习计划: 1.1周看基本知识(2014/1/18-2014/1/25) 2.基本知识总结(2014/1/26) 3.半周按教程写hel ...
- JQuery相关的网络资源
jquery插件列表 国外网站:http://plugins.jquery.com/ 国内网站:http://www.oschina.net/project/tag/273/jquery
- Extjs中numberfield小数位数设置
在默认的情况下,使用numberfield控件时只会显示两位小数,有的时候需要根据业务来确定显示小数的位数.通过设置下面的属性可以达到我们想要的目的: text : '存煤量(万吨)', dataIn ...