SpringBoot---SpringBoot项目整合fastjson

简介: SpringBoot---SpringBoot项目整合fastjson

导入fastjson依赖

1.剔除SpringBoot自带json格式

<!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2.引入fastjson

<!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

fastjson配置类

package studio.banner.officialwebsite.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.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author: Re
 * @Date: 2021/5/20 17:39
 */
@Configuration
public class FastjsonConfig {
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        //1、定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2、添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //3、解决浏览器乱码
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        //4、在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //5、将convert添加到converters中
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
}

若是需要进行其他配置请参考菜鸟教程


相关文章
|
8月前
|
JSON Java fastjson
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——使用 fastJson 处理 null
本文介绍如何使用 fastJson 处理 null 值。与 Jackson 不同,fastJson 需要通过继承 `WebMvcConfigurationSupport` 类并覆盖 `configureMessageConverters` 方法来配置 null 值的处理方式。例如,可将 String 类型的 null 转为 &quot;&quot;,Number 类型的 null 转为 0,避免循环引用等。代码示例展示了具体实现步骤,包括引入相关依赖、设置序列化特性及解决中文乱码问题。
435 0
|
JSON fastjson Java
Spring Boot Jackson 和Fast JSON 用哪个好啊 ?
【4月更文挑战第22天】
2895 1
|
fastjson Java 数据格式
springboot使用fastjson格式化日期数据不生效
springboot使用fastjson格式化日期数据不生效
springboot使用fastjson格式化日期数据不生效
|
JSON fastjson Java
SpringBoot 使用 FastJson 返回 JSON 数据时过滤部分字段
SpringBoot 使用 FastJson 返回 JSON 数据时过滤部分字段
638 0
|
JSON fastjson Java
SpringBoot 使用Fastjson 将返回值进行格式化处理
SpringBoot 使用Fastjson 将返回值进行格式化处理
676 0
|
Java fastjson 数据格式
SpringBoot使用fastjson的JsonField注解序列化Bigdecimal
SpringBoot使用fastjson的JsonField注解序列化Bigdecimal
10711 2
|
NoSQL Java fastjson
spring boot redis 自定义序列化方式(fastJson)
spring boot redis 自定义序列化方式(fastJson)
4756 0
|
JSON fastjson Java
SpringBoot换用FastJson:这是你喜欢的乱码~%?…,# *'☆&℃$︿★?
1.What:什么是SpringBoot? SpringBoot Banner SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。
3013 0
|
监控 druid fastjson
spring boot2 整合(一续)Druid数据库连接池和FastJson
本项目教程环境建立在spring boot2 整合(一)Mybatis (特别完整!) 的基础上。 1. Druid配置 1.1 修改pom.xml com.
1667 0
下一篇
oss云网关配置