springboot静态资源映射不生效问题

最近有个同事问我,静态资源映射不生效的问题,很正常我想不就是配置下资源路径就可以了吗?类似配置如下代码

java 复制代码
@Configuration
public class CorsConfig implements WebMvcConfigurer {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
		//一旦自定义配置静态资源路径,那么springboot默认的那四个静态资源路径规则就会失效
        registry.addResourceHandler("/images/**")
                .addResourceLocations("classpath:/static/","classpath:/static/images/","classpath:/static/images/movie/2023/07/08/","classpath:/static/images/cinema/2023/07/03/","classpath:/static/images/user/2023/07/08/")
                .setCachePeriod(100)
                .resourceChain(true)
                .addResolver(new PathResourceResolver());

    }
}

但通过敲击浏览器 127.0.0.1:port/images/1.jpg,却始终给我404。通过查阅资料和查看文件路径结构,发现我忽略了maven 中对于静态资源的打包方式,

我的目录结果

但是最终在项目的target/class目录下 却没有将static目录,那么问题很明显,尽管我配置了静态资源映射规则,但是实际打包的时候,却没有对应将资源文件打包进去!那么如何将文件打包进去呢?那就是maven中的配置

xml 复制代码
    <build>
        <defaultGoal>compile</defaultGoal>
        <!--定义资源路径-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

相关描述在此 maven文档说明

运行boot项目 ,最终在本地的target目录下的生成的路径如下

![(https://i-blog.csdnimg.cn/direct/aa3f6a6a894746c596c715efe03824c3.png)

重启项目 图片能正确访问到了

相关推荐
极光代码工作室35 分钟前
基于SpringBoot的校园论坛系统
java·springboot·web开发·后端开发
XS0301061 小时前
Spring Bean 作用域 & 生命周期
java·后端·spring
NagatoYukee1 小时前
Spring Security基础部分学习
java·学习·spring
彦为君1 小时前
JavaSE-07-异常机制
java·开发语言·后端·python·spring
_Aaron___2 小时前
Spring AI 接入 MCP:工具调用不是“能调就行”,关键是边界治理
java·人工智能·spring
我是一颗柠檬2 小时前
【MySQL全面教学】MySQL性能优化实战Day13(2026年)
数据库·后端·sql·mysql·性能优化·database
向量引擎2 小时前
从零起步,如何打造专属向量引擎 API 中转工作流?
java·服务器·前端
LJianK12 小时前
普通接口,用到getter和setter方法的地方,jackson转换
java
辰海Coding2 小时前
MiniSpring框架学习-分解 Dispatcher
java·学习·spring·架构