我们知道静态资源文件放在四个特点的位置可以访问到,现在我们想放在我们自定义包下,然后通过路径也能访问到。
现在我们在resources文件下创建javaboy文件夹,在文件夹下创建hello.js文件
第一种
在application.properties文件下配置静态资源访问路径
spring.resources.static-locations=classpath:/javaboy/
启动访问路径:http://localhost:8080/hello.js
访问成功
第二种
第二种方式就是自己写配置类
package org.javaboy.staticresource.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/javaboy/");
}
}
这种方式是添加了springboot中为我们添加的静态资源访问路径
访问路径:http://localhost:8080/hello.js
同样可以成功访问到