栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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配置文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.7
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    



四、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方式打包并没有将静态资源打包,所以无法访问静态资源

pom
六、场景及解决方案 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:SoftWareWallpaper
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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/837165.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号