[JAVA] Static file access priority in Spring boot

A note about static file access in spring-boot.

Locations where static files can be placed in Spring boot

spring boot is the default setting, projectDirectory/src/main/resources/META-INF/resources/ projectDirectory/src/main/resources/ projectDirectory/src/main/resources/static/ projectDirectory/src/main/resources/public/
By placing it below, access is possible.

As an example, When HTML is placed in each directory as shown below

project root
  └─ src
      └─ main
          └─ resources
              ├─ static
              |   └─ html
              |       └─ 1.html
              ├─ public
              |   └─ html
              |       └─ 2.html
              ├─ resources 
              |   └─ html
              |       └─ 3.html
              └─ META-INF
                  └─ resources
                      └─ html
                          └─ 4.html

To each HTML, http://localhost:8080/1.html http://localhost:8080/2.html http://localhost:8080/3.html http://localhost:8080/4.html It becomes possible to access in the form of.

Static file access priority in Spring boot

When a file with the same name exists in each directory as shown below

project root
  └─ src
      └─ main
          └─ resources
              ├─ static
              |   └─ html
              |       └─ test.html
              ├─ public
              |   └─ html
              |       └─ test.html
              ├─ resources 
              |   └─ html
              |       └─ test.html
              └─ META-INF
                  └─ resources
                      └─ html
                          └─ test.html

When you access http: //localhost:8080/test.html, META-INF / resources / html / test.html is returned

The search order for static files is

  1. resources/META-INF/resources/
  2. resources/
  3. resources/static/
  4. resources/public/

The search is performed in the order of, and the one found first is returned on the screen.

Think about using it in a project

  1. resources/META-INF/resources/
  2. resources/

Does not seem to be suitable for placing static files, as it also contains configuration files.

When actually using it in a project,

  1. resources/static/
  2. resources/public/

I think it will be placed in.

By name,

Something that doesn't change, such as a library, in resources / static /. resources / public / Place project-specific css, js, etc.

Is it like that? .. ..

Hmmm. I don't know what is best practice around here, so I don't know if this is all right. Someone who teaches and knows (; ∀ ;)

Recommended Posts

Static file access priority in Spring boot
Local file download memorandum in Spring Boot
How to bind to property file in Spring Boot
Specify the encoding of static resources in Spring Boot
Spring Boot 2 multi-project in Gradle
File upload with Spring Boot
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Spring Boot External setting priority
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
Write test code in Spring Boot
spring boot access authorization RESTful API
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
Implement Spring Boot application in Gradle
Thymeleaf usage notes in Spring Boot
Fitted in Spring Boot using a bean definition file named application.xml
What is a Spring Boot .original file?
Launch (old) Spring Boot project in IntelliJ
Output Spring Boot log in json format
Create Java Spring Boot project in IntelliJ
Loosen Thymeleaf syntax checking in Spring Boot
[Practice! ] Display Hello World in Spring Boot
Use DynamoDB query method in Spring Boot
DI SessionScope Bean in Spring Boot 2 Filter
How to split Spring Boot message file
Change session timeout time in Spring Boot
File output bean as JSON in spring
Procedure to make the value of the property file visible in Spring Boot
SameSite cookie in Spring Boot (Spring Web MVC + Tomcat)
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Output request and response log in Spring Boot
Challenge Spring Boot
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
How to add a classpath in Spring Boot
Java tips-Create a Spring Boot project in Gradle
Spring Boot Form
How to set environment variables in the properties file of Spring boot application
Spring Boot Memorandum
gae + spring boot
The attached file name was garbled in the Spring Boot email, so take measures
Annotations used in Spring Boot task management tool
[Spring Boot] How to refer to the property file
View the Gradle task in the Spring Boot project
Include external jar in package with Spring boot2 + Maven3
[Note] Configuration file when using Logback with Spring Boot
I checked asynchronous execution of queries in Spring Boot 1.5.9
Access the war file in the root directory of Tomcat
Include image in jar file with java static method
Access the built-in h2db of spring boot with jdbcTemplate
How to create a Spring Boot project in IntelliJ
SSO with GitHub OAuth in Spring Boot 1.5.x environment
How to use CommandLineRunner in Spring Batch of Spring Boot
Test field-injected class in Spring boot test without using Spring container
Until you start development with Spring Boot in eclipse 1
How to test file upload screen in Spring + Selenium
Try gRPC in Spring Boot & Spring Cloud project (Mac OS)
Introduce swagger-ui to REST API implemented in Spring Boot
Until you start development with Spring Boot in eclipse 2