官方文档网址:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

    Spring Boot为Spring MVC提供了自动配置,可与大多数应用程序完美配合:

    • 包含 ContentNegotiatingViewResolverBeanNameViewResolver
      • 自动配置了一个 ViewResolver(视图解析器)
      • 视图解析器:根据方法的返回值得到视图对象 View,然后视图对象决定如何渲染(转发或重定向),ContentNegotiatingViewResolver 用于组合所有的视图解析器
      • 自定义视图解析器:定义一个实现了 ViewResolver 接口的 Bean,Spring Boot会自动将该 Bean 组合进去
    • 支持静态资源,包括 WebJars 的支持
    • 自动注册 Converter、GenericConverter、Formatter
      • Converter:类型转换器
      • Formatter:格式化器 ```java // WebMvcProperties 有许多静态的配置内部类,如 Servlet、View、Format // 因此在主配置文件中可以通过 spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss 来配置日期格式 public static class Format { private String date; private String time; // yyyy-MM-dd HH:mm:ss private String dateTime; }

    // WebMvcAutoConfiguration @Bean @Override public FormattingConversionService mvcConversionService() { Format format = this.mvcProperties.getFormat(); // 注册时间日期转换器 WebConversionService conversionService = new WebConversionService(new DateTimeFormatters() .dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime())); addFormatters(conversionService); return conversionService; } ```

    • 支持 HttpMessageConverters
      • HttpMessageConverters:Spring MVC用来转换http请求和响应,默认情况下配置了将对象自动转换为 JSON(Jackson库)
      • 自定义 HttpMessageConverter 转换器:实现 HttpMessageConverter 接口并注册到 Spring 中(使用 @Bean 或 @Component 注解)
    • 自动注册 MessageCodesResolver:定义错误代码生成规则
    • 静态 index.html 支持
    • 定制 Favicon 支持
    • 自动使用 ConfigurableWebBindingInitializer :可以自定义配置