FastJson与Jackson,修改SpringBoot默认的JSON

版权声明:转载请注明出处与链接。 https://blog.csdn.net/With_Her/article/details/81979550

JSON

在前后台数据通信过程中,json数据格式是一种比较常用的方式。将javabean转化为json格式字符串,可以通过简单的字符串拼接

JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式。

JSON有两种表示结构,对象和数组。

  • 对象结构以”{”大括号开始,以”}”大括号结束。{ key1:value1, key2:value2, ... }
  • 数组结构以”[” 开始,”]” 结束。[ { key1:value1, key2:value2 }, { key3:value3, key4:value4 } ]

一般我们常用的JSON序列化与反序列化工具的为

  • FastJson

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>1.2.15</version>

</dependency>

  • Jackson

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-asl</artifactId>

<version>1.9.13</version>

</dependency>

 java对象到json字符串(序列化)性能对比:

  • 序列化时,在少量数据时,jackson性能比fastson要好,当数据量越来越大时,fastson的性能要好于jackson;序列化时选取何种json库,可根据数据多少进行选择。

 json字符串转换成java对象(反序列化)性能对比:

  • 反序列化时,除在map转化有些不同,在少量数据时,jackson性能比fastson要好,当数据量越来越大时,fastson的性能要好于jackson

 

SpringBoot默认内置的是jackson

如果想SpringBoot默认使用FastJson,一般有两种方式:

方式一、启动类继承WebMvcConfigurerAdapter ,复写configureMessageConverters


/**
 * 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.
 */
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter {
 
	// 第一种方式配置使用FstJson
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    	super.configureMessageConverters(converters);
		
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
 
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat
        );
        fastConverter.setFastJsonConfig(fastJsonConfig);
		
    	converters.add(fastConverter);
	}
 
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

 

方式二、添加Bean到Spring容器,进行管理

package com.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

/**
 * 设置config类 使用 @Bean注入 fastJsonHttpMessageConvert
 */
@Configuration
public class MassageConverConfiguration {
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定义一个 convert 转换消息的对象;
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3、在convert中添加配置信息.
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
}

不论是FastJson还是Jackson,我们在开发中,一般都会将FastJson/Jackson进行封装为JSONUtil

 

参考文档:

https://blog.csdn.net/yeputi1015/article/details/73554761/

https://www.jianshu.com/p/fca9cbe2453b

 

猜你喜欢

转载自blog.csdn.net/With_Her/article/details/81979550