Skip to content

Commit

Permalink
feat: MappingJackson2HttpMessageConverter 自定义扩展配置 & ObjectMapper 默认配置注入
Browse files Browse the repository at this point in the history
  • Loading branch information
fxbin committed Aug 1, 2023
1 parent d7ae0cc commit 03c22de
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<HttpMessageConverter<?>> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
cn.fxbin.bubble.fireworks.web.autoconfigure.jackson.CustomizeJacksonAutoConfiguration,\
cn.fxbin.bubble.fireworks.web.autoconfigure.CustomizeWebAutoConfiguration

0 comments on commit 03c22de

Please sign in to comment.