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 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛. 采用完全独立于编 ...
随机推荐
- ASP.NET Core 2.0 MVC 发布部署--------- ASP.NET Core 发布的具体操作
ASP.NET Core 发布的具体操作 下面使用C# 编写的ASP.NET Core Web项目示例说明发布的全过程. 1.创建项目 选择“文件” > “新建” > “项目”. 在“添加 ...
- javascript初步了解
0.1 <script> 和 </script> 会告诉 JavaScript 在何处开始和结束. <script> 和 </script> 之间的 ...
- systemd基础
systemd 起源 systemd这一名字源于Unix中的一个惯例:在Unix中常以“d”作为系统守护进程(英语:daemon,亦称后台进程)的后缀标识.除此以外,systemd亦是借代英文术语D体 ...
- hdu 5505(数论-gcd的应用)
GT and numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- numpy,看不懂,但很利害
慢慢有感觉.. 截几个图看看. import numpy as np np.random.seed(42) complex_numbers = np.random.random(5) + 1j * n ...
- git用法大全
转载自实验楼,之前有更新过两篇git的文章,毕竟内容太少,而git还有很多更丰富的技能,在实验楼上有一系列全的教程,这里做一下备案.需要时查阅. Git 实战教程 目录 一.实验说明 二.git的初始 ...
- 浅谈css中浮动和清除浮动带来的影响
有很多时候,我们都会用到浮动,而我们有时候对浮动只是一知半解,却不是太清楚它到底是怎么回事,不知道各位有没有和我一样的感觉,只知道用它,却不知道它到底是怎么回事,所以,在学习的过程中,就要把一个概念不 ...
- bzoj 1880 最短路径图
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...
- 利用linux判断elf文件是64位还是32位
readelf 命令,参数为-h 例如 文件名为python >>>readelf -h python 得到的是ELF Header中的项Magic 第五个数 02时为64位,01时 ...
- async await 使用笔记
JavaScript的网络请求异步的,即网络请求不会阻塞当前 js 代码的继续执行,而是通过回调的方式,网络请求的代码块中注入回调函数,当网络请求完成,会触发相应的事件,通过触发事件来执行注册的回调函 ...