• Spring Boot 配置静态资源路径


    默认路径

    在Spring Boot 2.7.2版本中,查看默认静态资源路径,在WebProperties.class中如下

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    
    • 1

    可以看到默认资源路径有4个。
    使用Spring Initializr新建Spring Boot项目,自带static目录,直接将前端资源文件放到该目录下,启动项目,访问http://localhost:端口号/资源目录/名称.html即可;
    例如,有一个front目录,该目录下存在一个index.html文件,将此目录放于src/main/resources/static下,并且未修改端口号,访问http://localhost:8080/front/index.html即可看到访问成功。

    修改路径

    使用配置文件进行修改

    对于低版本,在配置文件application.yml中如下:

    spring:
      resources:
        static-locations: classpath:/
    
    • 1
    • 2
    • 3

    代表将资源目录直接放在src/main/resources/
    但是,对于高版本,该方式已弃用,不推荐!!!

    对于高版本,在配置文件application.yml中如下:

    spring:
      web:
        resources:
          static-locations: classpath:/
    
    • 1
    • 2
    • 3
    • 4

    高版本这样设置,可以成功访问http://localhost:8080/front/index.html

    使用配置类进行修改

    新建配置类WebMvcConfig.java继承WebMvcConfigurationSupport

    package com.aiw.waimai.config;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    
    @Slf4j
    @Configuration
    public class WebMvcConfig extends WebMvcConfigurationSupport {
        /**
         * 设置静态资源映射
         * @param registry
         */
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            log.info("开始进行静态资源映射。。。");
            registry.addResourceHandler("/**").addResourceLocations("classpath:/");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    可以成功访问http://localhost:8080/front/index.html
    注意:两种配置方式不可同时存在,并且修改后默认的访问路径就失效了;对于配置类方式,@Slf4j是Lombok提供的注解,方便打印日志,非必须

    【更新】网上看到WebMvcConfigurationSupport已过时,故更新为实现WebMvcConfigurer接口

    package com.aiw.waimai.config;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Slf4j
    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
        /**
         * 设置静态资源映射
         *
         * @param registry
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            log.info("开始进行静态资源映射。。。");
            registry.addResourceHandler("/**").addResourceLocations("classpath:/");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    五、05【Java IO模型】之BIO NIO AIO介绍
    linux软件安装
    两个读书笔记:springboot+vue.js分布式组件全栈开发训练营 + 大数据开发基础
    数据结构与算法之堆: Leetcode 347. 前K个高频元素 (Typescript版)
    K8s源码分析(22)-client go组件之clientset
    Vue2.7正式发布!代号为:Naruto(火影忍者),原生支持 Composition API +终于可以在Vue2项目中使用Vue3的新特性了,真香~
    spring cloud生态中Feign、Ribbon、loadbalancer的一些历史
    2022.6.30-----leetcode.1175
    猿创征文|超实用的前端开发工具分享
    MySQL bit类型增加索引后查询结果不正确案例浅析
  • 原文地址:https://blog.csdn.net/qq_45917176/article/details/126300050