SpringBoot下配置jackson,配置不生效

 一、springboot中配置jackson有2中方式,yml中配置和配置类中配置。

以下示例主要以在全局配置返回对象进行序列号转json时忽略null和空字符为例:

(1)yml配置文件中配置,如下图所示:


(2)在配置类中配置



正常情况下,以上两种方式均可实现。但是有时候突然会出现配合失灵的情况,这是为什么呢,可能原因可能有2,加上了配置@EnableWebMvc 或配置类继承了WebMvcConfigurationSupport等关于webmvc的相关配置,这都会使得原有配置失效,看下官方关于这个的解释:

public class WebMvcConfigurationSupport extends Object implements ApplicationContextAware, ServletContextAware

This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding @EnableWebMvc to an application@Configuration class. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add@Configuration to the subclass and @Bean to overridden @Bean methods. For more details see the Javadoc of @EnableWebMvc.

This class registers the following HandlerMappings:

RequestMappingHandlerMapping ordered at 0 for mapping requests to annotated controller methods.

HandlerMapping ordered at 1 to map URL paths directly to view names.

BeanNameUrlHandlerMapping ordered at 2 to map URL paths to controller bean names.

HandlerMapping ordered at Integer.MAX_VALUE-1 to serve static resource requests.

HandlerMapping ordered at Integer.MAX_VALUE to forward requests to the default servlet.

Registers these HandlerAdapters:

RequestMappingHandlerAdapter for processing requests with annotated controller methods.

HttpRequestHandlerAdapter for processing requests with HttpRequestHandlers.

SimpleControllerHandlerAdapter for processing requests with interface-based Controllers.

Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:

ExceptionHandlerExceptionResolver for handling exceptions through @ExceptionHandler methods.

ResponseStatusExceptionResolver for exceptions annotated with @ResponseStatus.

DefaultHandlerExceptionResolver for resolving known Spring exception types

Registers an AntPathMatcher and a UrlPathHelper to be used by:

the RequestMappingHandlerMapping,

the HandlerMapping for ViewControllers

and the HandlerMapping for serving resources

Note that those beans can be configured with a PathMatchConfigurer.

Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:

a ContentNegotiationManager

a DefaultFormattingConversionService

a OptionalValidatorFactoryBean if a JSR-303 implementation is available on the classpath

a range of HttpMessageConverters depending on the third-party libraries available on the classpath.


出现如上问题的解决方法就是再次自定义messageConvert后问题即可解决,如下所示



增加jackson对LocalDate和LocalDateTime的序列化时,可增加如下配置


你可能感兴趣的:(SpringBoot下配置jackson,配置不生效)