SpringMVC杂记(1) 使用阿里巴巴的fastjson
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。
------------------------------------------------------------------------------------------------------------------------------------------------------------------
1) 国产开源软件要支持的
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.1</version>
</dependency>
2) spring没有提供相应的HttpMessageConverter可以自己写一个。
package com.alibaba.fastjson.spring.support; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset; import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature; 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();
} }
3) 配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true" >
<bean class="com.alibaba.fastjson.spring.support.MappingFastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="serializerFeature">
<array>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
SpringMVC杂记(1) 使用阿里巴巴的fastjson的更多相关文章
- Springmvc使用阿里巴巴的fastjson传输到前台中文乱码的解决方案,他娘的大家都少制造垃圾,学习过程将会多么快乐
弄了大概七八个小时吧 都他妈比的抄来抄去,一分一秒的去试错 最终参考这个问题解决了乱码的情况https://bbs.csdn.net/topics/392169549 412 需要在springmvc ...
- SpringMVC:JSON形式输出(基于Fastjson)
在Spring3.0中,@ResponseBody标记可以将对象"封装"为JSON形式的数据,并输出,下面的例子中使用的是阿里的Fastjson JSONaz解析工具,在sprin ...
- 解析Json的谷歌官方方法Gson和阿里巴巴的fastJson方法。
//测试单个json文本 public void testGsonTwo(){ String jsonStr = "{\"id\":100,\"name\&qu ...
- 阿里巴巴json fastjson String转javaBean
private Entity getEntity(String resp){ JSONObject jsonObj = (JSONObject) JSON.parse(resp); ...
- 阿里巴巴fastJson
FastJson解析 一.阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java ...
- springmvc与fastjson的整合,注解@RequestBody的使用
项目内容用的是jetty框架,传输数据格式是json格式,有一天我心血来潮,把项目又搭建了一次,完了,卡在了数据传输的格式上,明明原来框架直接用fastjson,但是我用就是不对,总是报fastjso ...
- SpringMVC整合fastjson、easyui 乱码问题
一.框架版本 SpringMVC:3.1.1.RELEASE fastjson:1.2.7 easyui :1.4.5 二.乱码现象 Action中使用@ResponseBody返回Json数据 ...
- SpringMVC中post请求参数注解@requestBody使用问题
一.httpClient发送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html public static String httpPostWithJ ...
- SpringMVC: JSON
SpringMVC:JSON讲解 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛. 采用完全独立于编 ...
随机推荐
- 【转载】移动开发中的上下左右滑动插件jquery.swipe.js
原文地址http://blog.csdn.net/pvfhv/article/details/3449803/# 源码: (function($) { var old = $.fn.swipe; $. ...
- 实习day2:@2X图片,git,coding.net,
@2X是5和6系列的图片,@3X是6P等大屏的图片 本公司目前只用@2X的图片适配. 比如20X27的图片 1x, 就是原始大小: 用2X, 就除以2,变成10X13.5: 如果用3X的, 就除以3, ...
- OPENSSL问题,使用fsockopen()函数提示错误
环境配置 系统环境 CentOS7.2WDCP v3.2.2 lanmp PHP 多版本 指定使用5.6 OpenSSL 1.0.2h 3 May 2016 php.ini相关设置allow_url ...
- linux中top命令
经常用到top命令,也就简单看看进程多不多,卡不卡, 这次在网上找到一个归纳总结的,以供参考. 简介 top 命令是 Linux 下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于 ...
- Java 性能优化手册 — 提高 Java 代码性能的各种技巧
转载: Java 性能优化手册 - 提高 Java 代码性能的各种技巧 Java 6,7,8 中的 String.intern - 字符串池 这篇文章将要讨论 Java 6 中是如何实现 String ...
- 【ASP.NET】:Ckeditor+Fckeditor的使用
首先这三个文件:下载ckeditor和ckeditor_aspnet_3.6.4和ckfinder 然后把这三个文件复制到项目根目录下 添加引用CKEditor.NET.dll CKFind ...
- Java 继承内部类
大家有没有想过内部类究竟能不能被继承呢? public class Main { public static void main(String[] args){ Outer outer = new O ...
- CentOS7 安装Python3.6.4
1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline- ...
- ElasticSearch6 报错FORBIDDEN/12/index read-only / allow delete (api)
FORBIDDEN/12/index read-only / allow delete (api) 官方解决方法: curl -XPUT -H "Content-Type: applicat ...
- Python入门基础知识(1) :locals() 和globals()
Python有两个内置的函数,locals() 和globals(),它们提供了基于字典的访问局部和全局变量的方式. 首先,是关于名字空间的一个名词解释.是枯燥,但是很重要,所以要耐心些.Python ...