Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Javascript
- java
- mysql
- error
- Python
- IntelliJ
- SQL
- isNotEmpty
- 리눅스
- Kibana
- datagrip
- 호이스팅
- 404error
- DBMS
- docker
- 자바
- pandas
- oracle
- spring
- 티스토리챌린지
- github
- git
- 명령어
- 오블완
- Linux
- iBatis
- zset
- PostgreSQL
- mssql
- analytics4
Archives
- Today
- Total
hanker
Spring Boot - static / templates (정적 파일 404 error 처리 방법) 본문
SPRING/SPRING-ERROR
Spring Boot - static / templates (정적 파일 404 error 처리 방법)
hanker 2025. 3. 31. 10:14반응형
Spring Boot 에서 파일을 생성하고 해당 파일들을 참조하거나 찾을 때 404Error 가 발생하는데,
정적 자원을 사용하기 위해서는 따로 설정을 해주어야 한다.

파일들을 추가하고 경로 지정하고 불러오려고 해도 404Error 가 발생하는 상황에 Configuration 클래스를 하나 만들어주면 해결된다.
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 MvcConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/", "classpath:/static/");
}
}
- WebMvcConfigurer를 구현해서 Spring MVC 관련 설정을 커스터마이징
- addResourceHandlers: 정적 자원(Static Resource)을 서빙하기 위한 경로를 설정하는 메서드
- addResourceHandler("/**"): 모든 URL 경로에 대해 리소스를 찾도록 설정
- addResourceLocations(...): 실제 리소스를 찾을 경로를 지정
반응형