From 03c22de911741b3f45f471832d5e981a4bcc21e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=97=B4=E5=B0=8F=E4=B9=99?= Date: Tue, 1 Aug 2023 22:09:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20MappingJackson2HttpMessageConverter=20?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=89=A9=E5=B1=95=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=20&=20ObjectMapper=20=E9=BB=98=E8=AE=A4=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomizeWebAutoConfiguration.java | 59 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/java/cn/fxbin/bubble/fireworks/web/autoconfigure/CustomizeWebAutoConfiguration.java diff --git a/bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/java/cn/fxbin/bubble/fireworks/web/autoconfigure/CustomizeWebAutoConfiguration.java b/bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/java/cn/fxbin/bubble/fireworks/web/autoconfigure/CustomizeWebAutoConfiguration.java new file mode 100644 index 00000000..8223c4ae --- /dev/null +++ b/bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/java/cn/fxbin/bubble/fireworks/web/autoconfigure/CustomizeWebAutoConfiguration.java @@ -0,0 +1,59 @@ +package cn.fxbin.bubble.fireworks.web.autoconfigure; + +import cn.fxbin.bubble.fireworks.core.module.JavaTimeModule; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.List; + +import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; +import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS; + +/** + * CustomizeWebAutoConfiguration + * + * @author fxbin + * @version v1.0 + * @since 2023/8/1 22:01 + */ +@Configuration( + proxyBeanMethods = false +) +@ComponentScan( + basePackages = {"cn.fxbin.bubble.fireworks.web"} +) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +public class CustomizeWebAutoConfiguration implements WebMvcConfigurer { + + @Override + public void extendMessageConverters(List> converters) { + MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false); + objectMapper.registerModule(new JavaTimeModule()); + messageConverter.setObjectMapper(objectMapper); + converters.add(0, messageConverter); + } + + @Bean + public ObjectMapper defaultObjectMapper() { + return new ObjectMapper() + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .configure(FAIL_ON_EMPTY_BEANS, false) + // 关闭异常提醒 + .configure(FAIL_ON_UNKNOWN_PROPERTIES, false) + .registerModule(new JavaTimeModule()); + } + +} \ No newline at end of file diff --git a/bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/resources/META-INF/spring.factories b/bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/resources/META-INF/spring.factories index a70c197b..d79e3b5d 100644 --- a/bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/resources/META-INF/spring.factories +++ b/bubble-fireworks-project/bubble-fireworks-starters/bubble-fireworks-starter-web/src/main/resources/META-INF/spring.factories @@ -3,4 +3,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ cn.fxbin.bubble.fireworks.web.autoconfigure.i18n.InternationalAutoConfiguration,\ cn.fxbin.bubble.fireworks.web.autoconfigure.GlobalHandlerAutoConfiguration,\ cn.fxbin.bubble.fireworks.web.autoconfigure.RestTemplateAutoConfiguration,\ -cn.fxbin.bubble.fireworks.web.autoconfigure.jackson.CustomizeJacksonAutoConfiguration \ No newline at end of file +cn.fxbin.bubble.fireworks.web.autoconfigure.jackson.CustomizeJacksonAutoConfiguration,\ +cn.fxbin.bubble.fireworks.web.autoconfigure.CustomizeWebAutoConfiguration \ No newline at end of file