티스토리 뷰
404 에러(not found)는 다른 에러와 다르게 dispatcher servlet에서 잡는다
@ControllerAdvice만으로 예외가 잡히지 않는다
spring boot : 2.6.8
추가로 해야할 작업
1. application.yml 에 추가
- 아래 web~ 을 하면 정적 리소스가 읽어지지 않음
- "No mapping found for HTTP request with URI" 에러 발생
spring:
mvc:
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false
2. WebConfig.java 에 정적 리소스 자원 경로 추가
- 위의 설정으로 css, js, image 와 같은 정적 파일들이 404 에러를 띄우며 찾아지지 않음
- 경로가 중요..
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//404 에러
registry
.addResourceHandler( "/images/**")
.addResourceLocations("classpath:/static/images/");
registry
.addResourceHandler("/css/**")
.addResourceLocations("classpath:/static/css/");
}
}
[오류해결] No mapping found for HTTP request with URI 에러
css와 js 등이 담긴 파일을 resources 폴더 안에 ad_assets와 assets로 나눈 구조로 프로젝트를 진행했다.그런데, No mapping found for HTTP request with URI 라는 에러가 발생했고 jsp파일을 실행해도 제대로 css
velog.io
https://yadon079.github.io/2021/spring%20boot/web-mvc-04
스프링 부트 활용 : 스프링 웹 MVC 4부 :: 개발자 한선우
스프링 부트 활용
yadon079.github.io
3. @ExceptionHandler 에 404 추가
@ControllerAdvice
public class ExceptionControllerAdvice {
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handle404Error(NoHandlerFoundException ex, Model model){
return "error/error400"; //custom error page
}
}
댓글