• SpringBoot扩展SpringMVC自动配置


    SpringBoot中自动配置了

    •   ViewResolver(视图解析器)
    •   ContentNegotiatingViewResolver(组合所有的视图解析器)
    •   自动配置了静态资源文件夹、静态首页、favicon.ico及Webjars
    •   Converter(转换器,转换类型使用)
    •        Formatter(格式化器)
    •        HttpMessageConverter(对SpringMVC的请求和响应进行序列化)
    •        MessageCodesResolver(定义错误代码生成规则)
    •        ConfigurableWebBindingInitializer(Web数据绑定器)

    我们可以自定义SpringMVC的配置

     1 package cn.coreqi.config;
     2 
     3 import org.springframework.context.annotation.Configuration;
     4 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
     5 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
     6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
     7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
     8 
     9 /**
    10  * 扩展SpringMVC
    11  * SpringBoot2使用的Spring5,因此将WebMvcConfigurerAdapter改为WebMvcConfigurer
    12  * 使用WebMvcConfigurer扩展SpringMVC好处既保留了SpringBoot的自动配置,又能用到我们自己的配置
    13  */
    14 //@EnableWebMvc //如果我们需要全面接管SpringBoot中的SpringMVC配置则开启此注解,
    15                 //开启后,SpringMVC的自动配置将会失效。
    16 @Configuration
    17 public class WebConfig implements WebMvcConfigurer {
    18     @Override
    19     public void addViewControllers(ViewControllerRegistry registry) {
    20         //设置对“/”的请求映射到index
    21         //如果没有数据返回到页面,没有必要用控制器方法对请求进行映射
    22         registry.addViewController("/").setViewName("index");
    23     }
    24 }
  • 相关阅读:
    PowerShell2.0之Windows排错(六)检查网络故障
    确保数据安全是云计算取信于用户的关键
    企业发展如何借助“云的力量”
    PowerShell2.0之维护网络(三)设置网络适配器
    Feign最佳实践
    Nacos注册中心原理
    GateWay网关快速入门
    Nacos集群搭建
    Feign快速入门
    Feign的性能优化
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10324465.html
Copyright © 2020-2023  润新知