springboot 项目中获取默认注入的序列化对象 ObjectMapper

在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration

在接口中经常需要使用时间类型,Date ,如果想要格式化成指定格式需要在 application.yml 配置文件中配置

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss.SSS
    time-zone: GMT+8
    defaultPropertyInclusion: non_null   #非空属性才序列化
    deserialization:
      FAIL_ON_UNKNOWN_PROPERTIES: false #未定义的key不序列化

AutoConfiguration 会将 ObjectMapper 注入到 spring 环境中,具体是在 JacksonAutoConfiguration 类中

 2 如果在 spring 环境中想要使用配置的 ObjectMapper ,使用自动注入方法即可

@Autowired
private ObjectMapper objectMapper;
原文地址:https://www.cnblogs.com/zhaopengcheng/p/10300279.html