12-20 개인 프로젝트 2일차

현보·2022년 12월 21일

오늘의 미션

/join , /login 기능 구현하기

ResponseEntity로 받는 것이 포스트맨,talend 같은 것으로 응답받기 위해 사용하는것.

/join

  • UserController에 @RestController, @RequiredArgsConstructor, @RequestMapping("/api/v1/users")
private final UserService userService;

@PostMapping("/join")
    public Response<UserJoinResponse> join(@RequestBody UserJoinRequest dto){
        UserDto userDto = userService.join(dto);
        return Response.success(new UserJoinResponse(userDto.getUserid(),userDto.getUserName()));
    }

에러처리가 제대로 안되는 현상

ExceptionManager에 예외처리를 위해 작성한 코드들이 console창에만 나오는 상황이 발생하여 검색을 하던중 @RestControllerAdvice 어노테이션을 안달아준것을 확인하고 해결하였습니다.

@RestControllerAdvice
public class ExceptionManager {

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<?> runtimeExceptionHandler(RuntimeException e){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(Response.error("ERROR",e.getMessage()));
    }

    @ExceptionHandler(UserAppException.class)
    public ResponseEntity<?> UserAppExceptionHandler(UserAppException e){
        ErrorResponse errorResponse = new ErrorResponse(e.getErrorCode(), e.getMessage());
        return ResponseEntity.status(e.getErrorCode().getStatus())
                .body(Response.error("ERROR", errorResponse));

    }
}

0개의 댓글