zoukankan      html  css  js  c++  java
  • WebMvcConfigurationSupport 避坑指南

      通过返回WebMvcConfigurationSupport 的方式, 默认会覆盖 Spring boot的自动配置, 导致配置失效静态资源无法访问:但是在WebMvcConfigurationadpter(已久过时)这是允许的

    @Bean
        public WebMvcConfigurationSupport initIndexConfig() {
    
           return new WebMvcConfigurationSupport() {
               @Override
               protected void addViewControllers(ViewControllerRegistry registry) {
                   registry.addViewController("/").setViewName("login");
                   registry.addViewController("/index.html").setViewName("login");
               }
    
               @Override
               protected void addResourceHandlers(ResourceHandlerRegistry registry) {
                   registry.addResourceHandler("/**")
                           .addResourceLocations("classpath:/resources")
                           .addResourceLocations("classpath:/static")
                           .addResourceLocations("classpath:/templates")
                           .addResourceLocations("classpath:/public");
               }
               @Override
               protected void addInterceptors(InterceptorRegistry registry) {
                   super.addInterceptors(registry);
               }
           };
       }

     WebMvcConfigurerAdapter 是一个适配器类,

    /** @deprecated */
    @Deprecated
    public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
        public WebMvcConfigurerAdapter() {
        }

     而WebMvcConfigurationSupport  提供默认实现 不是一个空方法

    public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
    
        private static final boolean romePresent;
    
        private static final boolean jaxb2Present;
    
        private static final boolean jackson2Present;
    
        private static final boolean jackson2XmlPresent;
    
        private static final boolean jackson2SmilePresent;
    
        private static final boolean jackson2CborPresent;

     所以: 采用返回 @Bean的方式, 会覆盖实现的很多默认配置导致不能使用,

    正确的使用方法是:

    @Configuration
    public class AppConfig extends WebMvcConfigurationSupport{
    
               @Override
               protected void addViewControllers(ViewControllerRegistry registry) {
                   registry.addViewController("/").setViewName("login");
                   registry.addViewController("/index.html").setViewName("login");
               }
    
               @Override
               protected void addResourceHandlers(ResourceHandlerRegistry registry) {
                   registry.addResourceHandler("/**")
                           .addResourceLocations("classpath:/resources")
                           .addResourceLocations("classpath:/static")
                           .addResourceLocations("classpath:/templates")
                           .addResourceLocations("classpath:/public");
               }
               @Override
               protected void addInterceptors(InterceptorRegistry registry) {
                   super.addInterceptors(registry);
               }

    你还可以选择 直接实现 

    WebMvcConfigurer;

    关于支持Webjar Swagger 等Jar包的资源路径
       /**
         * 资源路径 映射
         */
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/resources")
                    .addResourceLocations("classpath:/static")
                    .addResourceLocations("classpath:/templates")
                    .addResourceLocations("classpath:/public");
            /**
             * 支持webjars
             */
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
            /**
             * 支持swagger
             */
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            super.addResourceHandlers(registry);
        }
     
  • 相关阅读:
    CentOS7防火墙
    [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
    二叉树系列
    二叉树系列
    [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
    动态规划小结
    [LeetCode] Populating Next Right Pointers in Each Node I, II
    [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
    二叉树系列
    [LeetCode] 数组的最长连续数, O(n)解法
  • 原文地址:https://www.cnblogs.com/dgwblog/p/11962497.html
Copyright © 2011-2022 走看看