Spring boot (10): spring boot uses FastJson

Alibaba FastJson is a Json processing toolkit, including "serialization" and "deserialization". It has the following features:
The fastest, tests show that fastjson has extremely fast performance, surpassing any other Java Json parser. Including Jack Johnson, who claims to be the fastest;
Powerful function, fully supporting Java Bean, collection, Map, date, Enum, template and introspection; independent, able to run directly in Java SE 5.0 or above; supporting Android; open source (Apache 2.0)

In addition, the default json converter of SpringBoot is Jackson

1, Dependence

<!-- fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2, Modify the default json converter

package cn.aduu.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zh
 * @ClassName cn.aduu.config.FastJsonConfiguration
 * @Description
 */
@Configuration
public class FastJsonConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //Dealing with the problem of Chinese random code
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

You can use Fastjson to convert JSON later

                                <link rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-10f5517761.css">
                                </div>

Reproduced from: http://blog.csdn.net/saytime

Tags: JSON Java Android Apache

Posted on Sat, 02 May 2020 17:19:11 -0700 by Twysted