xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.dl.framework.config;
X 2
3 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
4 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
5 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
6 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
7 import com.dl.framework.jackson.BigNumberSerializer;
8 import lombok.extern.slf4j.Slf4j;
9 import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12
13 import java.math.BigDecimal;
14 import java.math.BigInteger;
15 import java.time.LocalDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.util.TimeZone;
18
19 /**
20  * jackson 配置
21  *
22  * @author Lion Li
23  */
24 @Slf4j
25 @Configuration
26 public class JacksonConfig {
27
28     @Bean
29     public Jackson2ObjectMapperBuilderCustomizer customizer() {
30         return builder -> {
31             // 全局配置序列化返回 JSON 处理
32             JavaTimeModule javaTimeModule = new JavaTimeModule();
33             javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
34             javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
35             javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
36           //  javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
37             DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
38             javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
39             javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
40             builder.modules(javaTimeModule);
41             builder.timeZone(TimeZone.getDefault());
42             log.info("初始化 jackson 配置");
43         };
44     }
45
46 }