spring boot 3 src/main/resources目录文件访问

1,199 阅读1分钟

目录结构图
  1. 静态资源默认访问顺序:classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
  2. 如果要自定义静态文件目录,必须在application.properties中配置,否则不能访问。例如新增test文件夹:,classpath:/test/
#  配置静态资源文件夹
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/

  1. 静态文件访问方式,直接是:域名+端口+子目录+静态文件名 注意:不需要默认的文件夹名,但如果里面建了子目录,则需要加上,例如:http://localhost:8080/test.js
  2. templates目录资源,不是静态资源,需要经过controller访问。例如:
    http://localhost:8080/api/loggin
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/api/")
public class FileController {
	
	@RequestMapping("loggin")
	public Object loggin() {
		//返回文件名
		return "loggin";
	}

}