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)

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

相关推荐
CoderYanger1 分钟前
递归、搜索与回溯-综合练习:19.目标和
java·算法·leetcode·1024程序员节
LSL666_2 分钟前
SpringBoot项目连接deepseek
java·spring boot·后端·deepseek
蒲公英源码3 分钟前
AI智慧教育平台架构设计与实现方案,基于Jdk17+SpringBoot3AI智慧教育平台
java·人工智能·mysql·jdk
i***39583 分钟前
JAVA系统中Spring Boot 应用程序的配置文件:application.yml
java·开发语言·spring boot
f***45323 分钟前
Spring Boot 经典九设计模式全览
java·spring boot·设计模式
tkevinjd5 分钟前
JavaHashMap源码分析
java·后端
l***37095 分钟前
在2023idea中如何创建SpringBoot
java·spring boot·后端
s***117011 分钟前
Spring+Quartz实现定时任务的配置方法
java
j***630816 分钟前
Spring Boot 多数据源解决方案:dynamic-datasource-spring-boot-starter 的奥秘(上)
java·spring boot·后端
z***677718 分钟前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot