Spring Boot-5

유호준·2021년 5월 15일
0

Spring Boot

목록 보기
6/7

이번엔 에러처리 관련 클래스를 만들어보자!


@ControllerAdvice

이전에 @ExceptionHadnler는 한 클래스만 담당했다면 @ControllerAdvice는 모든 Controller의 에러를 담당할 수 있도록 하는 Annotation입니다.


ControllerExceptionHandler 생성

원래 PostController 클래스의 메소드를 옮깁니다.

@ControllerAdvice
public class ControllerExceptionHandler {
    @ExceptionHandler(value = RuntimeException.class)
    private ResponseEntity<?> errorHandler(){
        Map<String,String> body = new HashMap<>();
        body.put("error","error");
        return ResponseEntity.ok(body);
    }
}

테스트

0개의 댓글