dev/Spring

Advice를 이용한 예외 처리

amuse1991 2019. 3. 21. 16:05


- 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();
}
}