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的序列化时,可增加如下配置


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,483评论 0 10
  • By clicking to agree to this Schedule 2, which is hereby ...
    qaz0622阅读 1,508评论 0 2
  • Today we are going to examine two economies that has deve...
    徐恩峰阅读 61评论 0 0
  • 一个不会说谎的女孩,一个具有真性情的女孩,一个敢于表达爱与不爱的女孩。这个女孩身上特有的一些东西在我们今天这个时代...
    林小圆May阅读 1,398评论 0 0
  • 【别离难】 一别重逢几多愁,引来诗友思悠悠。 相见时难别亦难,待等明朝在青州。 大树好乘凉2018.08.09 注...
    交朋识友阅读 429评论 5 11