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