springboot 404的解决方法
1,如果你本身想访问的是一个静态资源 先确定项目的输出路径下是否存在该资源,maven项目的输出路径一般在target/classess,在idea中如下图所示:
若不存在,需要在maven中添加配置,将该资源文件在编译时输出到classpath下,一般可添加如下代码在pom.xml中:
<build> <resources> <resource> <directory>src/main/java</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
若资源存在,那就是:1,静态资源映射配置错误;2,你的项目本身没有静态资源相关的配置,使用的是springboot默认的配置,但你的资源没有放在默认的文件夹下,默认的文件夹有:
"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"
那么如何自定义静态文件映射呢:可以在application.yml中加入以下配置
#静态资源存放路径 spring: #配置classpath 下 static和mystatic两个文件夹为静态资源文件夹 resources: static-locations: classpath:/mystatic,classpath:/static #静态资源url映射路径 mvc: static-path-pattern: /static/** # 自定义配置会覆盖默认配置,注意key、value之间一定要有空格 # 在上面的配置中如果访问localhost/static/a.jpg,则会依次在mystatic和static两个文件下查找a.jpg,找到直接返回。如果是 localhost/static/a/b.jpg则会相应的去查找对应的文件夹
在上面的配置修改好后,一定要检查:1,key value之间是否有空格,否则配置不生;2,相应的重启服务器,缓存也会影响访问结果;3,路径是否是以配置的static作为静态资源的访问父路径
3, 如果你使用了thymeleaf模板,一定要保证引入了相关依赖并保证配置正确 在pom.xml中加入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
在application.yml中加入
spring: thymeleaf: cache: false #默认情况下是templates,这里改成了view文件夹 prefix: classpath:/view/ suffix: .html
下一篇:
什么时候选择mmap而非read?