【springboot】之 解析@EnableWebMvc 、WebMvcConfigurationSupport和WebMvcConfigurationAdapter
WebMvcConfigurationAdapter在spring boot2.0新版本中已经被废弃了,因为WebMvcConfigurationAdapter中的默认实现方法已经在它的父类中实现了。新版本spring boot是基于jdk8实现的,所以支持在接口中添加默认实现方法。
springboot默认格式化日期只需要在application文件中配置
spring.jackson.date-format= yyyy-MM-dd HH:mm:ss spring.jackson.time-zone= GMT+8 spring.jackson.locale= zh_CN
但是,我在配置过程中发现日期格式依然是long类型,也就是日期格式没有生效
经过查看代码发现,我的MvcConfig继承了WebMvcConfigurationSupport
那么这个类到底做什么呢?导致application配置的json格式失效呢?
如下:
在spring-boot+spring mvc 的项目中,有些时候我们需要自己配置一些项目的设置,就会涉及到这三个,那么,他们之间有什么关系呢?
首先,@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法
总结有以下几种使用方式:
- @EnableWebMvc,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置,完全被spring mvc框架控制,而且关闭了ComponentScan,只有一个BeanNameViewResolver视图解析器,默认配置了中央处理器DispatchServlet,还有加一个静态资源处理器DefaultServletHandlerConfigurer.enable()。当然,你可以自己重写customizeRegistration设置多文件上传,设置启动参数setInitParameter,重写getServletFilters添加过滤器,重写onStartup设置启动优先级,添加过滤器,添加servlet。
@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration { }
- extends WebMvcConfigurationSupport,在扩展的类中重写父类的方法即可,这种方式会屏蔽springboot的@EnableAutoConfiguration中的设置
@Configuration @EnableWebMvc @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyHttpMessageConverter()); } }
-
implements WebMvcConfigurer,在扩展的类中重写父类的方法即可,但是不要加@EnableWebMvc。这种方式依旧使用springboot的@EnableAutoConfiguration中的设置,还可以添加spring mvc框架中的一些配置,比如(过滤器等),这个是spring boot 2.0官方推荐配置
@Configuration @ComponentScan(basePackageClasses = MyConfiguration.class) public class MyConfiguration implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry formatterRegistry) { formatterRegistry.addConverter(new MyConverter()); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyHttpMessageConverter()); } }
具体哪种方法适合,看个人对于项目的需求和要把控的程度
在WebMvcConfigurationSupport(@EnableWebMvc)和@EnableAutoConfiguration这两种方式都有一些默认的设定
参考:
https://www.cnblogs.com/gyjx2016/p/8494498.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2018-03-21 关于Java类加载双亲委派机制的思考(附一道面试题)
2018-03-21 Tomcat8源码编译及导入Eclipse中研究
2018-03-21 maven项目install时候一直报错,检查Maven-javadoc-plugin声明错误(Java 8 配置Maven-javadoc-plugin)或者是:警告: @throws 没有说明
2018-03-21 maven-gpg-plugin:1.2:sign (sign-artifacts) on project jdbc-pool: Cannot obtain passphrase in batch mode或者是Plugin execution not covered by lifecycle configuration
2018-03-21 org.eclipse.jdt.internal.compiler包下的类找不到