升级到spring boot2.0后,继承WebMvcConfi
2018-08-05 本文已影响166人
李北北
使用spring boot2.0搭建一个项目,当配置一个@EnableWebMvc类的时候发现WebMvcConfigurerAdapter类已经过时了,如图:

去查阅资料发现替代类是WebMvcConfigurationSupport,于是就改成了继承WebMvcConfigurationSupport类,如图:

但是使用该类配置的首页却找不到默认的路径 / ,访问路径【http://localhost:8081/common/】结果如下:

最后查找资料发现是@EnableWebMvc注解与继承WebMvcConfigurationSupport类会导致一些问题,于是我尝试了两种方案,最后都可以解决问题。
方案一:去掉@EnableWebMvc注解,代码如下:

方案二:保留@EnableWebMvc注解,但是继承WebMvcConfigurationSupport类改成实现WebMvcConfigurer接口,代码如下:

最后都可以成功访问:

参考:
1、 @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别
2、springboot2.0、spring5.0 拦截器配置WebMvcConfigurerAdapter过时使用WebMvcConfigurationSupport来代替 新坑
3、 @EnableWebMvc