web 개발/spring

<Spring> Exception

잼추 2023. 1. 15. 21:33

1. runtimeException  을 상속받은 exceiption class 생성

@Getter
public class CustomValidationException extends RuntimeException {
    private Map<String, String> errorMap;

    public CustomValidationException(String message, Map<String, String> errorMap) {
        super(message);
        this.errorMap = errorMap;
    }
}

 

2. ExceiptionAdvice class 생성

@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(CustomValidationException.class)
    public ResponseEntity<?> validationError(CustomValidationException e) {
        return ResponseEntity.badRequest()
                .body(new CMRespDto<>(HttpStatus.BAD_REQUEST.value(), "Validation Error", e.getErrorMap()));
    }
}

@ RestControllerAdvice, ExceiptionHandler(exception class.class) 받고

return 값으로 bad request !

'web 개발 > spring' 카테고리의 다른 글

<spring> MVC, thymeleaf  (0) 2024.05.15
<spring> swagger ui  (0) 2023.02.20
<spring> REST API  (0) 2023.01.12
<spring> get, post  (0) 2022.12.12
<spring>DI(denpendency injection) , IOC  (0) 2022.12.12