目录
  • springboot2.x过后static下的静态资源无法访问

    springboot 2.x 里面访问静态资源的坑

    在spring boot的自定义配置类继承 webmvcconfigurationsupport 后,发现自动配置的静态资源路径

    classpath:/meta/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

    不生效。

    首先看一下 自动配置类的定义:

    这是因为在 springboot的web自动配置类 webmvcautoconfiguration 上有条件注解

    @conditionalonmissingbean(webmvcconfigurationsupport.class) 

    这个注解的意思是在项目类路径中 缺少 webmvcconfigurationsupport类型的bean时改自动配置类才会生效,所以继承 webmvcconfigurationsupport 后需要自己再重写相应的方法。

    如果想要使用自动配置生效

    又要按自己的需要重写某些方法,比如增加 viewcontroller ,则可以自己的配置类可以继承 webmvcconfigureradapter 这个类。不过在spring5.0版本后这个类被丢弃了 webmvcconfigureradapter ,虽然还可以用,但是看起来不好。

    /**
     * 原来是这么写的:
     * public class beanconfiguration extends webmvcconfigurationsupport
     * 导致默认配置的静态资源不生效了
     */
    @configuration
    public class beanconfiguration implements webmvcconfigurer {
        @bean
        public mappingjackson2httpmessageconverter jackson2httpmessageconverter() {
            mappingjackson2httpmessageconverter converter = new mappingjackson2httpmessageconverter();
            objectmapper mapper = new objectmapper();
            mapper.configure(deserializationfeature.fail_on_unknown_properties, false);
            mapper.setdateformat(new simpledateformat("yyyy-mm-dd hh:mm:ss"));
            mapper.settimezone(timezone.gettimezone("gmt+8"));
            mapper.setdefaultpropertyinclusion(jsoninclude.include.always);
            converter.setobjectmapper(mapper);
            return converter;
        }
        @override
        public void configuremessageconverters(list<httpmessageconverter<?>> converters) {
            //将我们定义的时间格式转换器添加到转换器列表中,
            //这样jackson格式化时候但凡遇到date类型就会转换成我们定义的格式
            converters.add(jackson2httpmessageconverter());
            // 添加字符串转换,否认如果返回字符串,则会报异常,其他converter 
            // 参考:org.springframework.web.servlet.config.annotation.webmvcconfigurationsupport#adddefaulthttpmessageconverters
            stringhttpmessageconverter stringhttpmessageconverter = new stringhttpmessageconverter();
            stringhttpmessageconverter.setwriteacceptcharset(false);  // see spr-7316
            converters.add(stringhttpmessageconverter);
        }
    }
    

    springboot2.x过后static下的静态资源无法访问

    package com.example.thymeleaf.commons; 
    import org.springframework.stereotype.component;
    import org.springframework.web.servlet.config.annotation.resourcehandlerregistry;
    import org.springframework.web.servlet.config.annotation.webmvcconfigurer;
     
    /**
     * 配置静态资源映射
     *
     * @author sunziwen
     * @version 1.0
     * @date 2018-11-16 14:57
     **/
    @component
    public class webmvcconfig implements webmvcconfigurer {
        /**
         * 添加静态资源文件,外部可以直接访问地址
         *
         * @param registry
         */
        @override
        public void addresourcehandlers(resourcehandlerregistry registry) {
            registry.addresourcehandler("/static/**").addresourcelocations("classpath:/static/");
        }
    }

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。