Spring Boot----SpringBoot中SpringMVC配置原理
官方文档
29.1.1 Spring MVC Auto-configuration
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
The auto-configuration adds the following features on top of Spring’s defaults:
- Inclusion of
ContentNegotiatingViewResolver
andBeanNameViewResolver
beans.- 自动配置ViewResolver(视图解析器:根据方法的返回值获得视图对象(View),视图对象决定如何渲染(转发?重定向?))
- ContentNegotiatingViewResolver组合了所有的视图解析器
- 如何定制?我们只需要给容器添加一个视图解析器,自动回将其组合进来
- Support for serving static resources, including support for WebJars (covered later in this document)).
- Automatic registration of
Converter
,GenericConverter
, andFormatter
beans.- Converter:转换器,比如public String test(User user);表单中传入的数字,true/false等都是文本,这些文本需要映射到User中,需要转换器,进行类型转换,将text(数字)-->int/integer,text(true)-->bool
Formatter
:格式化,2017/0/101--->Date
- Support for
HttpMessageConverters
(covered later in this document).- HttpMessageConverters:是SpringMVC用来http请求和响应的,比如将User--->json返回
- HttpMessageConverters是从容器中确定的
- Automatic registration of
MessageCodesResolver
(covered later in this document). - Static
index.html
support. - Custom
Favicon
support (covered later in this document). - Automatic use of a
ConfigurableWebBindingInitializer
bean (covered later in this document).
Spring Boot扩展 SpringMVC功能
@EnableWebMvc
spring mvc如果需要使用注解配置spring mvc,
@Configuration @EnableWebMVc //需要加注解 public class MyMvcConfig implements WebMvcConfigurer{ //不需要实现WebMvcConfigurer了 @Bean public WebMvcConfigurer webMvcConfigurer(){ WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer(){ @Override public void addInterceptors(InterceptorRegistry registry) { //拦截所有的请求,springboot帮我们已经做好了静态资源映射,所以我们可以不加excludePathPatterns("/static/**"),但是如果出现被拦截了,那就手动放行 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").excludePathPatterns("/static/**"); } }; return webMvcConfigurer; } }
在spring MVC中,我们可以编写xml来配置我们需要的一些功能,比如拦截器等等。
you can add your own @Configuration
class of type WebMvcConfigurer
but without @EnableWebMvc
我们可以在类型是WebMvcConfigurer的类上使用@Configuration注解,并且实现我们需要的方法;
class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("test"); return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } } @Configuration public class MyMvcConfig implements WebMvcConfigurer{ //不需要实现WebMvcConfigurer了 @Bean public WebMvcConfigurer webMvcConfigurer(){ WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer(){ @Override public void addInterceptors(InterceptorRegistry registry) { //拦截所有的请求,springboot帮我们已经做好了静态资源映射,所以我们可以不加excludePathPatterns("/static/**"),但是如果出现被拦截了,那就手动放行 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").excludePathPatterns("/static/**"); } }; return webMvcConfigurer; } }
测试的时候,直接实现方法也是可以的。所以上面的 MyMvcConfig就不需要实现 WebMvcConfigurer 方法了
@Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/xx"); } }
Spring Boot全面接管SpringMVC功能
@Configuration public class MyMvcConfig implements WebMvcConfigurer { /** * 注册启动页面 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } /** * 配置跨域 * @param corsRegistry */ @Override public void addCorsMappings(CorsRegistry corsRegistry) { corsRegistry.addMapping("/*/**").allowedHeaders("*").allowedMethods("*").allowCredentials(true) .maxAge(1800).allowedOrigins("*"); } /** * 设置json数据返回格式 * @return */ @Bean MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); //objectMapper.setPropertyNamingStrategy(new MyNameStrategy()); objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper); return mappingJackson2HttpMessageConverter; } }