Spring Boot 使用fastjson序列化对象

1、spring boot默认使用jackson对json序列化和发序列化

可在配置文件中增加默认的时间格式化方式:

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
public class MrType {

  @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
  @DateTimeFormat(pattern="yyyy-MM-dd")
  private Date createdDate;
}

2、使用fastjson对json数据序列化和发序列化

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

@SpringBootApplication
public class MonitorRedbagMqApplication {

    public static void main(String[] args) {
        SpringApplication.run(MonitorRedbagMqApplication.class, args);
    }

   
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        //FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //fastConverter.setFastJsonConfig(fastJsonConfig);
        //HttpMessageConverter<?> converter = fastConverter;
        //return new HttpMessageConverters(converter);

        //创建fastJson消息转换器
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        //创建配置类
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //过滤并修改配置返回内容
        fastJsonConfig.setSerializerFeatures(
                //List字段如果为null,输出为[],而非null
                SerializerFeature.WriteNullListAsEmpty,
                //字符类型字段如果为null,输出为"",而非null
                SerializerFeature.WriteNullStringAsEmpty,
                //Boolean字段如果为null,输出为false,而非null
                //SerializerFeature.WriteNullBooleanAsFalse,
                //消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
                SerializerFeature.DisableCircularReferenceDetect,
                //是否输出值为null的字段,默认为false。
                SerializerFeature.WriteMapNullValue
        );
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);

        HttpMessageConverter<?> converter = fastJsonConverter;
        return new HttpMessageConverters(converter);
    }

}

3、测试Java对象

public class MonitorRedbagDto implements Serializable {
    private static final long serialVersionUID = -1600920246142453543L;

    // 红包ID
    private Integer redbagId;
    // 用户ID
    private Long userId;
    // 发放时间
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date sendTime;

    // setter getter方法
}

如果不用fastjson序列化,返回:

{
  "redbagId": null,
  "userId": 16,
  "sendTime": "2018-03-06T12:04:15.483+0000"
}

使用fastjson序列化,返回:

{
  "userId": 16,
  "sendTime": "2018-03-06 12:04:15"
}

 

 

 

转载于:https://my.oschina.net/u/3777515/blog/1630634

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值