美文网首页
日常解决bug-springboot webMvcConfig

日常解决bug-springboot webMvcConfig

作者: iukcy祝福 | 来源:发表于2021-03-04 09:36 被阅读0次
    矮袋鼠

    在SpringBoot1.X的版本中,我们可以继承自WebMvcConfigurerAdapter,覆盖想要实现的方法即可,但是在新版中建议停用。

    spring boot2.0之后在构造spring配置文件时建议推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport ,经测试实现WebMvcConfigurer是没问题,但继承WebMvcConfigurationSupport类是会导致自动配置失效的。

    针对上述问题提供两种解决办法:

    第一种:使用implements WebMvcConfigurer(推荐)
    /**
     * web 配置
     * iukcy
     * 2021-02-23 14:45:53
     * 备注:会自动引入默认实现
     */
    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
        static final String ORIGINS[] = new String[]{"GET", "POST", "PUT", "DELETE"};
    
        //静态资源映射
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/static/")
                    .addResourceLocations("classpath:/static/img/")
                    .addResourceLocations("file:/Users/zhangzhufu/upload/");
            registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                    "classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
    
        // AJAX请求跨域
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/remote/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS).maxAge(3600);
        }
    
    第二种:使用extends WebMvcConfigurationSupport
    /**
     * web 配置
     * zhangzhufu
     * 2021-02-23 14:45:53
     * 备注:继承WebMvcConfigurationSupport 并不会将自动实现自动引入过来, 
     * 需要全部重写
     */
    @Configuration
    public class WebMvcConfig extends WebMvcConfigurationSupport {
        static final String ORIGINS[] = new String[]{"GET", "POST", "PUT", "DELETE"};
    
        //静态资源映射
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/static/")
                    .addResourceLocations("classpath:/static/img/")
                    .addResourceLocations("file:/Users/zhangzhufu/upload/");
            registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                    "classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
    
        // AJAX请求跨域
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/remote/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS).maxAge(3600);
        }
    }
    
    两者区别:(@ConditionalOnClass和@ConditionalOnMissingBean用法和区别)
    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnWebApplication(
        type = Type.SERVLET
    )
    @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
    @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
    @AutoConfigureOrder(-2147483638)
    @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
    public class WebMvcAutoConfiguration {
        public static final String DEFAULT_PREFIX = "";
        public static final String DEFAULT_SUFFIX = "";
        private static final String[] SERVLET_LOCATIONS = new String[]{"/"};
    
        public WebMvcAutoConfiguration() {
        }
    

    相关文章

      网友评论

          本文标题:日常解决bug-springboot webMvcConfig

          本文链接:https://www.haomeiwen.com/subject/kqmhqltx.html