Commit 3435f0e70d6c9b54c949afa1dcb75a37b9f780a1
1 parent
8072ee56
issues/I561IU 3.2版本,跑测试用例代码抛出异常
json格式化代码优化
Showing
1 changed file
with
42 additions
and
29 deletions
jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/WebMvcConfiguration.java
1 | 1 | package org.jeecg.config; |
2 | 2 | |
3 | +import com.fasterxml.jackson.core.JsonGenerator; | |
4 | +import com.fasterxml.jackson.databind.DeserializationFeature; | |
3 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
4 | -import com.fasterxml.jackson.databind.module.SimpleModule; | |
5 | -import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; | |
6 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
7 | +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; | |
6 | 8 | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; |
9 | +import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; | |
10 | +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; | |
7 | 11 | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; |
12 | +import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; | |
8 | 13 | import io.micrometer.prometheus.PrometheusMeterRegistry; |
9 | 14 | import org.springframework.beans.factory.InitializingBean; |
15 | +import org.springframework.beans.factory.annotation.Autowired; | |
10 | 16 | import org.springframework.beans.factory.annotation.Value; |
11 | 17 | import org.springframework.beans.factory.config.BeanPostProcessor; |
12 | 18 | import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository; |
13 | -import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; | |
14 | 19 | import org.springframework.context.annotation.Bean; |
15 | 20 | import org.springframework.context.annotation.Conditional; |
16 | 21 | import org.springframework.context.annotation.Configuration; |
22 | +import org.springframework.context.annotation.Primary; | |
17 | 23 | import org.springframework.http.converter.HttpMessageConverter; |
18 | 24 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
19 | 25 | import org.springframework.web.cors.CorsConfiguration; |
... | ... | @@ -22,7 +28,11 @@ import org.springframework.web.filter.CorsFilter; |
22 | 28 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
23 | 29 | import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; |
24 | 30 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
31 | + | |
32 | +import java.text.SimpleDateFormat; | |
33 | +import java.time.LocalDate; | |
25 | 34 | import java.time.LocalDateTime; |
35 | +import java.time.LocalTime; | |
26 | 36 | import java.time.format.DateTimeFormatter; |
27 | 37 | import java.util.List; |
28 | 38 | |
... | ... | @@ -42,6 +52,9 @@ public class WebMvcConfiguration implements WebMvcConfigurer { |
42 | 52 | @Value("${spring.resource.static-locations}") |
43 | 53 | private String staticLocations; |
44 | 54 | |
55 | + @Autowired(required = false) | |
56 | + private PrometheusMeterRegistry prometheusMeterRegistry; | |
57 | + | |
45 | 58 | /** |
46 | 59 | * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片、视频、音频等 |
47 | 60 | */ |
... | ... | @@ -80,37 +93,38 @@ public class WebMvcConfiguration implements WebMvcConfigurer { |
80 | 93 | urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); |
81 | 94 | return new CorsFilter(urlBasedCorsConfigurationSource); |
82 | 95 | } |
83 | - | |
84 | - /** | |
85 | - * 添加Long转json精度丢失的配置 | |
86 | - * @Return: void | |
87 | - */ | |
88 | 96 | @Override |
89 | 97 | public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { |
90 | - MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); | |
91 | - ObjectMapper objectMapper = new ObjectMapper(); | |
92 | - SimpleModule simpleModule = new SimpleModule(); | |
93 | - simpleModule.addSerializer(Long.class, ToStringSerializer.instance); | |
94 | - simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); | |
95 | - objectMapper.registerModule(simpleModule); | |
96 | - jackson2HttpMessageConverter.setObjectMapper(objectMapper); | |
98 | + MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(objectMapper()); | |
97 | 99 | converters.add(jackson2HttpMessageConverter); |
98 | 100 | } |
99 | 101 | |
100 | 102 | /** |
101 | - * 解决springboot2.6 | |
102 | - * 日期时间格式化 | |
103 | - * @return | |
103 | + * 自定义ObjectMapper | |
104 | 104 | */ |
105 | 105 | @Bean |
106 | - public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { | |
107 | - return builder -> { | |
108 | - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | |
109 | - //返回时间数据序列化 | |
110 | - builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(formatter)); | |
111 | - //接收时间数据反序列化 | |
112 | - builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)); | |
113 | - }; | |
106 | + @Primary | |
107 | + public ObjectMapper objectMapper() { | |
108 | + ObjectMapper objectMapper = new ObjectMapper(); | |
109 | + //处理bigDecimal | |
110 | + objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); | |
111 | + objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); | |
112 | + //处理失败 | |
113 | + objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); | |
114 | + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
115 | + objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false); | |
116 | + objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES, false); | |
117 | + //默认的处理日期时间格式 | |
118 | + objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); | |
119 | + JavaTimeModule javaTimeModule = new JavaTimeModule(); | |
120 | + javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | |
121 | + javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); | |
122 | + javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); | |
123 | + javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | |
124 | + javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); | |
125 | + javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); | |
126 | + objectMapper.registerModule(javaTimeModule); | |
127 | + return objectMapper; | |
114 | 128 | } |
115 | 129 | |
116 | 130 | /** |
... | ... | @@ -124,12 +138,11 @@ public class WebMvcConfiguration implements WebMvcConfigurer { |
124 | 138 | |
125 | 139 | |
126 | 140 | /** |
127 | - * 解决springboot2.6 | |
128 | 141 | * 解决metrics端点不显示jvm信息的问题(zyf) |
129 | 142 | */ |
130 | 143 | @Bean |
131 | - InitializingBean forcePrometheusPostProcessor(BeanPostProcessor meterRegistryPostProcessor, PrometheusMeterRegistry registry) { | |
132 | - return () -> meterRegistryPostProcessor.postProcessAfterInitialization(registry, ""); | |
144 | + InitializingBean forcePrometheusPostProcessor(BeanPostProcessor meterRegistryPostProcessor) { | |
145 | + return () -> meterRegistryPostProcessor.postProcessAfterInitialization(prometheusMeterRegistry, ""); | |
133 | 146 | } |
134 | 147 | |
135 | 148 | } |
... | ... |