[SpringBoot] 회원가입 예외처리 하기(statusCode:400) + 인텔리제이 디버깅
회원가입 시 username과 password의 구성이 알맞지 않으면 에러메시지와 statusCode: 400을 Client에 반환하기
요구사항에 맞게 코드를 작성하려 한다.
UserController.java
@PostMapping("/user/signup")
public ResponseEntity<ApiResponseDto> signup(@RequestBody @Valid SignupRequestDto requestDto) {
try{
userService.signup(requestDto);
}catch (IllegalArgumentException e){
return ResponseEntity.badRequest().body(new ApiResponseDto("중복된 username입니다.", HttpStatus.BAD_REQUEST.value()));
}
return ResponseEntity.status(201).body(new ApiResponseDto("회원가입 성공", HttpStatus.CREATED.value()));
}
SignupRequestDto.java
@Pattern(regexp = "^[0-9a-z]{4,10}$",
message = "최소4자 이상, 10자 이하의 알파벳 소문자(a~z), 숫자(0~9)로 구성된 username을 입력해주세요.")
@NotBlank(message = "username을 입력해주세요.")
private String username;
@Pattern(regexp = "^[a-zA-Z0-9!@#$%^&*()_+{}:\"<>?,.\\\\/]{8,15}$",
message = "최소 8자 이상, 15자 이하의 알파벳 대소문자(a~z)(A~Z), 숫자(0~9), 특수문자로 구성된 password를 입력해주세요.")
@NotBlank(message = "password를 입력해주세요.")
private String password;
private UserRoleEnum role;
위의 @Valid 정규표현식을 사용하면 에러 상황일 때 반환 코드가 403 Forbidden이다.
이 상태코드를 400으로 반환해야 한다.
포스트맨을 사용하여 확인해 보았다.
password를 정규표현식에 맞지 않게 보냈을때 403 Forbidden 상태코드가 보인다.
아래는 콘솔에서 보이는 경고 로그이다.
WARN 7524 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<com.sparta.blog.ApiResponseDto> co m.sparta.blog.user.controller.UserController.signup(com.sparta.blog.user.dto.SignupRequestDto) with 2 errors: [Field error in object 'signupRequestDto' on field 'password': rejected value []; codes [NotBlank.signupRequestDto.password,NotBlank.password,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [signupRequestDto.password,password]; arguments []; default message [password]]; default message [password를 입력해주세요.]] [Field error in object 'signupRequestDto' on field 'password': rejected value []; codes [Pattern.signupRequestDto.password,Pattern.password,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [signupRequestDto.password,password]; arguments []; default message [password],[Ljakarta.validation.constraints.Pattern$Flag;@4fa2507c,^[a-zA-Z0-9!@#$%^&*()_+{}:"<>?,.\\/]{8,15}$]; default message [최소 8자 이상, 15자 이하의 알파벳 대소문자(a~z)(A~Z), 숫자(0~9), 특수문자로 구성된 password를 입력해주세요.]] ]
DefaultHandlerExceptionResolver가 MethodArgumentNotValidException이 발생했다고 알려준다.
ExceptionHandler를 구현할 것이다.
ExceptionHandler는 해당 클래스에서 에러가 발생했을 때
{MethodArgumentNotValidException.class}가 handleMethodArgumentNotValidException 메서드를 실행 함으로써 에러처리를 공통화하겠다는 뜻이다.
@ExceptionHandler({MethodArgumentNotValidException.class})
public ResponseEntity<ApiResponseDto> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
}
이제 안에 해당 메시지가 출력될 때 상태코드는 400으로 반환할 것이야.라는실행 로직을 작성해줘야 한다.
해당 메시지를 가져오기 위해선 안에 값이 어떻게 들어가는지 디버깅을 해본다.
똑같이 포스트맨 보내고 콘솔을 보면 ex → bindingResult → errors → 0번째 → 해당 메시지 우클릭 add to watches
이동된 곳에서 우클릭 edit을 하면 이렇게 경로가 나온다.
@ExceptionHandler({MethodArgumentNotValidException.class})
public ResponseEntity<ApiResponseDto> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
ApiResponseDto restApiException = new ApiResponseDto(ex.getBindingResult().getAllErrors().get(0).getDefaultMessage(), HttpStatus.BAD_REQUEST.value());
return new ResponseEntity<>(restApiException, HttpStatus.BAD_REQUEST);
}
이 경로를 msg란에 적어주면 끝.
ApiResponseDto에는 msg와 statusCode 변수가 들어있다.
포스트맨에서 확인해 보면 400을 잘 반환해주고 있다.