Springboot整合FastJson
Posted Bockpecehhe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot整合FastJson相关的知识,希望对你有一定的参考价值。
fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件。它是目前json解析最快的开源组件没有之一!
我们进行Springboot整合FastJson
一:添加依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.33</version> </dependency>
添加依赖我们编写自己的类来继承
@Configuration public class FastJsonConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = fastJsonHttpMessageConverter.getFastJsonConfig(); List<MediaType> fastMediaTypes = new ArrayList(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonConfig.setSerializerFeatures( SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue ); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastJsonHttpMessageConverter); } }
三:自己的JSON类
package org.spring.springboot.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import java.io.Serializable; import java.util.List; public class MyJson<T> implements Serializable{ public static final int FAIL = 0; public static final int SUCCESS = 1; public static final int TOKEN = 2; public static final int OTHER = 9; private static final long serialVersionUID = -8528102467884030980L; private String msg = "success"; private int code = 1; private T data; public MyJson(T data) { this.data = data; } public <E> E converDataEntity(Class<E> clazz) { return JSON.parseObject(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString}), clazz); } public JSONObject converDataJSONObject() { return JSON.parseObject(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString})); } public <E> List<E> converDataArray(Class<E> clazz) { return JSON.parseArray(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString}), clazz); } public JSONArray converDataJSONArray() { return JSON.parseArray(JSON.toJSONString(this.data, new SerializerFeature[]{SerializerFeature.WriteNonStringValueAsString})); } public String getMsg() { return this.msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return this.code; } public void setCode(int code) { this.code = code; } public T getData() { return this.data; } public void setData(T data) { this.data = data; } }
以上是关于Springboot整合FastJson的主要内容,如果未能解决你的问题,请参考以下文章
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段