이번엔 에러처리 관련 클래스를 만들어보자!
이전에 @ExceptionHadnler는 한 클래스만 담당했다면 @ControllerAdvice는 모든 Controller의 에러를 담당할 수 있도록 하는 Annotation입니다.
원래 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);
}
}