-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: MappingJackson2HttpMessageConverter 自定义扩展配置 & ObjectMapper 默认配置注入
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
.../main/java/cn/fxbin/bubble/fireworks/web/autoconfigure/CustomizeWebAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters