2023년 05월 27일

@VERO
Created Date · 2023년 05월 27일 13:05
Last Updated Date · 2023년 05월 27일 13:05

LOG

  • 2시에 matzip 회의가 있었다. 루루, 체인저, 애슐리, 주드와 오찌, 블링, 오리가 참석했다. 나는 에어팟이 없어서 250만원짜리 전화기를 썼다... 근데 진짜 하나도 안 들려서 그냥 정리해준 거 봤다. 체인저 나이스. 생산적인 회의였다.
  • 지하철 미션 제출했다. 리뷰 온 지 4일만에 죽은 자 가운데서 다시 태어나시고.. 하나의 쿼리로 모든 노선을 조회할 수 있도록 변경한 게 뿌듯했다. 근데 다시 돌아보면 이런 간단한 걸 왜 못했을까 싶긴 했다. 아무래도 select * 를 쓰면 조인이 제대로 안 됐는데 조회하는 컬럼의 이름을 지정해주면 조인이 잘 된다.

배운 것

  • @Positive 를 컨트롤러 @PathVariable에 적어줄 수 있다. 다음과 같이 사용할 수 있다.
@Validated  
@RestController  
@RequestMapping("/path")  
public class PathController {  
    private final PathService pathService;  
  
    public PathController(final PathService pathService) {  
        this.pathService = pathService;  
    }  
  
    @GetMapping("/{startStationId}/{endStationId}")  
    public ResponseEntity<ShortestPathResponse> findShortestPath(  
            @PathVariable("startStationId") @Positive(message = "역 ID는 양수여야 합니다.") final Long startStationId,  
            @PathVariable("endStationId") @Positive(message = "역 ID는 양수여야 합니다.") final Long endStationId  
    ) {  
        ShortestPathResponse shortestPathResponse = pathService.findShortestPath(startStationId, endStationId);  
        return ResponseEntity.ok(shortestPathResponse);  
    }  
}

이때 리턴하는 예외는 ConstraintViolationException 이다. 해당 예외를 처리하기 위해 @ControllerAdvice 에 다음과 같이 작성해주었다.

@ExceptionHandler(ConstraintViolationException.class)  
public ResponseEntity<ErrorResponse> handleNotFoundException(ConstraintViolationException e) {  
    List<String> errorMessages = List.of(e.getMessage().split(": "));  
    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorResponse(errorMessages.get(1)));  
}

ConstraintViolationException 예외의 메시지는 findShortestPath: "역 ID는 양수여야 합니다." 라고 전달되기 때문에 파싱해주는 작업을 추가로 진행하였다.

궁금한 것

  • ConstraintViolationException 메시지는 무조건 저런 형태로 나오나?

느낀 것