十一、SpringBootWeb开发-SpringMVC所有常用特性配置
# 1. 全面接管SpringMVC
SpringBoot 默认配置好了 SpringMVC 的所有常用特性。
如果我们需要全面接管SpringMVC的所有配置并禁用默认配置,仅需要编写一个
WebMvcConfigurer配置类,并标注@EnableWebMvc即可全手动模式
@EnableWebMvc: 禁用默认配置
WebMvcConfigurer组件:定义MVC的底层行为
# 1. WebMvcAutoConfiguration 到底自动配置了哪些规则
SpringMVC自动配置场景给我们配置了如下所有默认行为
WebMvcAutoConfigurationweb场景的自动配置类支持RESTful的filter:HiddenHttpMethodFilter
支持非POST请求,请求体携带数据:FormContentFilter
导入
EnableWebMvcConfiguration:RequestMappingHandlerAdapterWelcomePageHandlerMapping: 欢迎页功能支持(模板引擎目录、静态资源目录放index.html),项目访问/ 就默认展示这个页面.RequestMappingHandlerMapping:找每个请求由谁处理的映射关系ExceptionHandlerExceptionResolver:默认的异常解析器LocaleResolver:国际化解析器ThemeResolver:主题解析器FlashMapManager:临时数据共享FormattingConversionService: 数据格式化 、类型转化Validator: 数据校验JSR303提供的数据校验功能WebBindingInitializer:请求参数的封装与绑定ContentNegotiationManager:内容协商管理器
WebMvcAutoConfigurationAdapter配置生效,它是一个WebMvcConfigurer,定义mvc底层组件定义好
WebMvcConfigurer底层组件默认功能;所有功能详见列表视图解析器:
InternalResourceViewResolver视图解析器:
BeanNameViewResolver,**视图名(controller方法的返回值字符串)**就是组件名内容协商解析器:
ContentNegotiatingViewResolver请求上下文过滤器:
RequestContextFilter: 任意位置直接获取当前请求静态资源链规则
ProblemDetailsExceptionHandler:错误详情- SpringMVC内部场景异常被它捕获:
定义了MVC默认的底层行为:
WebMvcConfigurer
# 2. @EnableWebMvc 禁用默认行为
@EnableWebMvc给容器中导入DelegatingWebMvcConfiguration组件,
他是 WebMvcConfigurationSupport
WebMvcAutoConfiguration有一个核心的条件注解,@ConditionalOnMissingBean(WebMvcConfigurationSupport.class),容器中没有WebMvcConfigurationSupport,WebMvcAutoConfiguration才生效.- @EnableWebMvc 导入
WebMvcConfigurationSupport导致WebMvcAutoConfiguration失效。导致禁用了默认行为
- @EnableWebMVC 禁用了 Mvc的自动配置
- WebMvcConfigurer 定义SpringMVC底层组件的功能类
# 2. WebMvcConfigurer 功能
定义扩展SpringMVC底层功能
| 提供方法 | 核心参数 | 功能 | 默认 |
|---|---|---|---|
| addFormatters | FormatterRegistry | 格式化器:支持属性上@NumberFormat和@DatetimeFormat的数据类型转换 | GenericConversionService |
| getValidator | 无 | 数据校验:校验 Controller 上使用@Valid标注的参数合法性。需要导入starter-validator | 无 |
| addInterceptors | InterceptorRegistry | 拦截器:拦截收到的所有请求 | 无 |
| configureContentNegotiation | ContentNegotiationConfigurer | 内容协商:支持多种数据格式返回。需要配合支持这种类型的HttpMessageConverter | 支持 json |
| configureMessageConverters | List<HttpMessageConverter<?>> | 消息转换器:标注@ResponseBody的返回值会利用MessageConverter直接写出去 | 8 个,支持byte,string,multipart,resource,json |
| addViewControllers | ViewControllerRegistry | 视图映射:直接将请求路径与物理视图映射。用于无 java 业务逻辑的直接视图页渲染 | 无 mvc:view-controller |
| configureViewResolvers | ViewResolverRegistry | 视图解析器:逻辑视图转为物理视图 | ViewResolverComposite |
| addResourceHandlers | ResourceHandlerRegistry | 静态资源处理:静态资源路径映射、缓存控制 | ResourceHandlerRegistry |
| configureDefaultServletHandling | DefaultServletHandlerConfigurer | 默认 Servlet:可以覆盖 Tomcat 的DefaultServlet。让DispatcherServlet拦截/ | 无 |
| configurePathMatch | PathMatchConfigurer | 路径匹配:自定义 URL 路径匹配。可以自动为所有路径加上指定前缀,比如 /api | 无 |
| configureAsyncSupport | AsyncSupportConfigurer | 异步支持: | TaskExecutionAutoConfiguration |
| addCorsMappings | CorsRegistry | 跨域: | 无 |
| addArgumentResolvers | List<HandlerMethodArgumentResolver> | 参数解析器: | mvc 默认提供 |
| addReturnValueHandlers | List<HandlerMethodReturnValueHandler> | 返回值解析器: | mvc 默认提供 |
| configureHandlerExceptionResolvers | List<HandlerExceptionResolver> | 异常处理器: | 默认 3 个 ExceptionHandlerExceptionResolver ResponseStatusExceptionResolver DefaultHandlerExceptionResolver |
| getMessageCodesResolver | 无 | 消息码解析器:国际化使用 | 无 |
# 2. 最佳实践
SpringBoot 已经默认配置好了Web开发场景常用功能。我们直接使用即可。
# 三种方式
| 方式 | 用法 | 效果 | |
|---|---|---|---|
| 全自动 | 直接编写控制器逻辑 | 全部使用自动配置默认效果 | |
| 手自一体 | @Configuration + 配置WebMvcConfigurer+ 配置 WebMvcRegistrations | 不要标注 @EnableWebMvc | 保留自动配置效果 手动设置部分功能 定义MVC底层组件 |
| 全手动 | @Configuration + 配置WebMvcConfigurer | 标注 @EnableWebMvc | 禁用自动配置效果 全手动设置 |
总结:
给容器中写一个配置类@Configuration实现 WebMvcConfigurer但是不要标注 @EnableWebMvc注解,实现手自一体的效果。
# 两种模式
1、前后分离模式: @RestController响应JSON数据
2、前后不分离模式:@Controller + Thymeleaf模板引擎