当前位置:   article > 正文

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

spring addresourcehandlers 404

一、硬件环境

Windows 11 21H2
  • 1

二、软件环境

Maven 3.8.1
jdk1.8.0_40
IntelliJ IDEA 2021.3.3
Spring Boot 2.6.7
  • 1
  • 2
  • 3
  • 4

三、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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

四、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、修改配置文件
(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/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
(2)发布本地目录

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

spring:
  mvc:
  	# 访问路径需要加前缀 http://127.0.0.1:8080/image/1.jpg
    static-path-pattern: /image/**
  web:
  	# 资源目录,多个用逗号分隔
    resources:
      static-locations: file:C:\SoftWare\Wallpaper
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

参考文献:

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/917489
推荐阅读
相关标签
  

闽ICP备14008679号