| | |
| | | package com.xinquan.common.security.config; |
| | | |
| | | import com.fasterxml.jackson.annotation.JsonInclude; |
| | | import com.fasterxml.jackson.databind.MapperFeature; |
| | | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.databind.module.SimpleModule; |
| | | import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; |
| | | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; |
| | | import java.math.BigInteger; |
| | | import java.time.format.DateTimeFormatter; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| | | import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
| | | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
| | | |
| | | |
| | | /** |
| | | * Jackson配置 |
| | | */ |
| | | @Configuration |
| | | @ConditionalOnClass(ObjectMapper.class) |
| | | public class JacksonConfig { |
| | | |
| | | @SuppressWarnings("deprecation") |
| | | |
| | | private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; |
| | | private static final String DATE_PATTERN = "yyyy-MM-dd"; |
| | | |
| | | @Bean |
| | | public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() { |
| | | final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); |
| | | builder.serializationInclusion(JsonInclude.Include.NON_NULL); |
| | | final ObjectMapper objectMapper = builder.build(); |
| | | SimpleModule simpleModule = new SimpleModule(); |
| | | // Long 转为 String 防止 js 丢失精度 |
| | | simpleModule.addSerializer(Long.class, ToStringSerializer.instance); |
| | | objectMapper.registerModule(simpleModule); |
| | | // 忽略 transient 关键词属性 |
| | | objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true); |
| | | return new MappingJackson2HttpMessageConverter(objectMapper); |
| | | public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { |
| | | return jacksonObjectMapperBuilder -> { |
| | | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern( |
| | | DATE_TIME_PATTERN); |
| | | jacksonObjectMapperBuilder.timeZone("GMT+8"); |
| | | jacksonObjectMapperBuilder.serializers(new LocalDateTimeSerializer(dateTimeFormatter)); |
| | | jacksonObjectMapperBuilder.deserializers( |
| | | new LocalDateTimeDeserializer(dateTimeFormatter)); |
| | | // long -> string |
| | | jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance); |
| | | jacksonObjectMapperBuilder.serializerByType(BigInteger.class, |
| | | ToStringSerializer.instance); |
| | | }; |
| | | } |
| | | } |