Spring MVC整合fastjson
来源:互联网 发布:暴漫官网淘宝 编辑:程序博客网 时间:2024/04/29 01:52
接到任务,需要springmvc整合fastjson,原因嘛,当然是为了快。
百度到的资料不全面,因此,自己总结一下,不妥的地方请大家指正。
1,首先引入fastjson的jar包
pom.xml
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.8</version></dependency>
2,在springmvc的配置中启用fastjson
<mvc:annotation-driven> <mvc:message-converters register-defaults="true" > <bean class="com.oversea.frame.MappingFastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="serializerFeature"> <array> <value>WriteMapNullValue</value> <value>QuoteFieldNames</value> </array> </property> </bean> </mvc:message-converters></mvc:annotation-driven>
对于这个,网上有另一种写法
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.oversea.frame.MappingFastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> </bean> </mvc:message-converters> </mvc:annotation-driven>
貌似两种方法用的人都有,但是有些前台的框架一定要html哪个,第一种是不会错的
3.添加MappingFastJsonHttpMessageConverter类。这个百度的教程里没说,我也是错了之后才发现的。
public class MappingFastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object>{ public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); // fastjson特性参数 private SerializerFeature[] serializerFeature; public SerializerFeature[] getSerializerFeature() { return serializerFeature; } public void setSerializerFeature(SerializerFeature[] serializerFeature) { this.serializerFeature = serializerFeature; } public MappingFastJsonHttpMessageConverter() { super(new MediaType("application", "json", DEFAULT_CHARSET)); } @Override public boolean canRead(Class<?> clazz, MediaType mediaType) { // JavaType javaType = getJavaType(clazz); // return this.objectMapper.canDeserialize(javaType) && // canRead(mediaType); return true; } @Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { // return this.objectMapper.canSerialize(clazz) && canWrite(mediaType); return true; } @Override protected boolean supports(Class<?> clazz) { // should not be called, since we override canRead/Write instead throw new UnsupportedOperationException(); } @Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i; while ((i = inputMessage.getBody().read()) != -1) { baos.write(i); } return JSON.parseArray(baos.toString(), clazz); } @Override protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { String jsonString = JSON.toJSONString(o, serializerFeature); OutputStream out = outputMessage.getBody(); out.write(jsonString.getBytes(DEFAULT_CHARSET)); out.flush(); }}4.配置现在就完了,接着就可以用了,用的时时候需要在用的函数上加上注解
@ResponseBody,这个表示 用我们配置好的东西返回json,接着,就可以直接return 对象啦
1 0
- Spring MVC整合fastjson
- Spring MVC 4 - 整合fastjson
- fastjson与spring mvc整合的配置
- Spring MVC 4.2+ 整合配置fastjson 1.2.11+
- Ajax进行POST提交,Spring MVC整合fastJson
- spring boot 整合 resteasy+fastjson
- spring-mvc中配置fastjson
- spring MVC-fastjson中文乱码
- Spring+Spring MVC整合
- spring mvc 环境Fastjson转换器jsonp请求
- Spring MVC + fastjson实现异步加载
- Spring MVC整合DWR
- Spring MVC 整合 Freemarker
- Spring MVC 整合Velocity
- Spring MVC整合DWR
- spring mvc 整合 velocity
- spring mvc整合velocity
- spring mvc + hibernate 整合
- 【Windows】32位和64位区别
- 二叉查找树BST
- RelativeLayout常用布局属性
- 欢迎使用CSDN-markdown编辑器
- android播放音乐
- Spring MVC整合fastjson
- #define private public
- Nginx+Keepalived实现站点高可用
- 码农小汪-剑指Offer之34 -两个链表的第一个公共结点 hashMap
- Android 修改阿拉伯语数字显示
- LeetCode *** 338. Counting Bits
- bzoj4404 Binary vs Decimal
- Node.js模板引擎: Jade入门
- WifiDog 认证原理和流程