Spring Boot 2.x 静态资源访问404问题整理记录

一、硬件环境

Windows 11 21H2

二、软件环境

Maven 3.8.1
jdk1.8.0_40
IntelliJ IDEA 2021.3.3
Spring Boot 2.6.7

三、POM配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

四、Spring Boot 静态资源目录

配置资源目录
/staticclasspath:/static/
/publicclasspath:/public/
/resourcesclasspath:/resources/
/META-INF/resourcesclasspath:/META-INF/resources/*

五、问题

1、打包方式

项目的打包类型分为:pom、jar、war

类型作用
pom父类型都为pom类型
jar内部调用或者是作服务使用
war需要部署的项目

POM是最简单的打包类型。不像一个JAR,SAR,或者EAR,它生成的构件只是它本身,没有代码需要测试或者编译,也没有资源需要处理,打包类型为POM的项目的默认目标。

生命周期阶段目标
packagesite:attach-descriptor
installinstall:install
deploydeploy:deploy

POM方式打包并没有将静态资源打包,所以无法访问静态资源

<packaging>pom</packaging>

六、场景及解决方案

1、修改配置文件
(1)访问本地静态资源

适用场景:前后端未分离项目或需要访问项目内部html、js、img等静态资源。

spring:
  mvc:
  	# 访问路径不需要加前缀 http://127.0.0.1:8080/1.jpg
    static-path-pattern: /**
  web:
  	# 资源目录,多个用逗号分隔
    resources:
      static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

(2)发布本地目录

适用场景:项目中有图片上传、在线浏览等场景,需要挂载并发布本地目录

spring:
  mvc:
  	# 访问路径需要加前缀 http://127.0.0.1:8080/image/1.jpg
    static-path-pattern: /image/**
  web:
  	# 资源目录,多个用逗号分隔
    resources:
      static-locations: file:C:\SoftWare\Wallpaper
2、实现WebMvcConfigurer
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
class MyWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 加载静态资源,访问路径不需要加前缀 http://127.0.0.1:8080/1.jpg
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 挂在本地目录,访问路径需要加前缀 http://127.0.0.1:8080/image/1.jpg
        registry.addResourceHandler("/image/**").addResourceLocations("file:C:/SoftWare/Wallpaper/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}

参考文献:

  • https://blog.csdn.net/qq_41094332/article/details/108497080
  • https://blog.csdn.net/syc000666/article/details/105110117
  • https://blog.csdn.net/xiaozheng12/article/details/100517099
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值