使用fastJson配置数据格式转换器

1、在pom.xml文件中引入fastJson模块

	<!--json工具类-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.31</version>
    </dependency>

2、编写配置类

 	import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.serializer.DoubleSerializer;
    import com.alibaba.fastjson.serializer.SerializeConfig;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.util.TypeUtils;
    import org.springframework.context.annotation.Configuration;
    
    import java.sql.Date;
    import java.sql.Timestamp;
    
    @Configuration
    public class MyFastJsonConfig extends FastJsonConfig {
    
        public MyFastJsonConfig() {
            SerializeConfig config = SerializeConfig.getGlobalInstance();
            //将数据库的Date格式数据转化为"yyyy-MM-dd"格式
            config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
            //将数据库的Timestamp格式数据转化为"yyyy-MM-dd HH:mm:ss"格式
            config.put(Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
            config.put(Double.class, new DoubleSerializer("#.################"));
            //config.config(JSON.class, SerializerFeature.WriteMapNullValue, true);
            this.setSerializeConfig(config);
    
            TypeUtils.compatibleWithJavaBean =true;
            JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteMapNullValue.getMask();
        }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个基于 Spring 的轻量级框架,可以快速搭建基于 Spring 的应用程序。而 Fastjson 是一个高性能的 Java 序列化和反序列化库,可以处理复杂的 JSON 数据。 如果想要在 Spring Boot 中自定义 Fastjson 作为 JSON 消息转换器,可以按照以下步骤进行操作: 首先,在 pom.xml 文件中引入 Fastjson 的依赖,可以通过以下代码来添加依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency> ``` 然后,在 Spring Boot 的配置类中配置 Fastjson 作为 JSON 消息转换器。可以通过以下代码来实现: ```java @Configuration public class FastjsonConfig { @Bean public HttpMessageConverters fastjsonHttpMessageConverter() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); converter.setDefaultCharset(Charset.forName("UTF-8")); List<MediaType> supportedMediaTypes = new ArrayList<>(); supportedMediaTypes.add(MediaType.APPLICATION_JSON); converter.setSupportedMediaTypes(supportedMediaTypes); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); converter.setFastJsonConfig(config); return new HttpMessageConverters(converter); } } ``` 最后,在 Spring Boot 的主类中加上 @EnableWebMvc 注解,启用自定义的 JSON 消息转换器。 通过以上步骤,就成功地使用了 Spring Boot 自定义 Fastjson 作为 JSON 消息转换器。这样可以方便地处理 JSON 数据,提升了系统的性能和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值