본문 바로가기

dev/Spring

Advice를 이용한 예외 처리


- IdAlreadyExistException

public class IdAlreadyExistingException extends RuntimeException {
public IdAlreadyExistingException(String userId){
super("user id \""+userId+"\" is already exist");
}
}


- UserExceptionAdvice

@ControllerAdvice
public class UsersExceptionAdvice {
@ResponseBody
@ExceptionHandler(IdAlreadyExistingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
String idAlreadyExistingHandler(IdAlreadyExistingException ex){
return ex.getMessage();
}
}