-
Notifications
You must be signed in to change notification settings - Fork 170
[4기 - 황창현] Week 3-2 REST API로 바우처 관리 구현, RestControllerAdvice를 통한 예외 처리 #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: changhyeon/w3-1
Are you sure you want to change the base?
Conversation
|
|
||
| @Slf4j | ||
| @ControllerAdvice | ||
| // @ControllerAdvice |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RestControllerAdvice에서도 동일한 클래스에 대한 예외 처리를 하고 있어서 @ControllerAdvice를 주석처리하여 사용하지 않도록 했습니다!
동일한 클래스에 대해서 ControllerAdvice와 RestControllerAdvice 2개를 동시에 사용할 수 있는지 확인해보았을 때 basePackage 설정을 통해 할 수는 있었으나 현재 디렉토리 구조성 불가하다고 판단되어 주석처리로 미사용 상태로 두었습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ControllerAdvice와 RestControllerAdvice 차이점은 무엇일까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ControllerAdvice는 에러처리 후 반환 값으로 ViewResolver를 통해 페이지를 반환하지만 RestControllerAdvice는 ResponseBody를 이용해 객체를 반환합니다!
Controller와 RestController처럼 Rest 붙고 안붙고의 차이는 @ResponseBody 인 것 같습니다!
그래서 위 처럼 생각했을 때 RestController API로 작동하는 URI의 경우 RestControllerAdvice를 통해 동작할 것이고
Controller API로 작동하는 URI의 경우 ControllerAdvice로 작동해서 서로 다르게 처리할 줄 알았지만 테스트해보니 무조건 Controller로 동작되는 것을 확인할 수 있었습니다! 그래서 위와같이 // 주석처리하여 RestControllerAdivce가 동작할 수 있도록 했습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇다면 제거?ㅎㅎ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
창현님 수고하셨습니다~!
thymeleaf 미션 PR과 중복되는 리뷰는 생략했습니다.
해당 PR부터 리팩토링 후 읽어주세요~~
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/customer") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rest api url 규칙을 읽어보시고, 반드시 지켜주세요!
구글링해서 아무거나 ... https://velog.io/@pjh612/REST-API-URI-%EA%B7%9C%EC%B9%99
아래 리뷰부터는 꼭 공부하시고 읽어주세요~
리소스는 복수형으로 써주세요
| @RequestMapping("/api/customer") | |
| @RequestMapping("/api/customers") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
생각보다 정말 많은 규칙이 있는데 너무 마음대로 사용했던 것 같습니다..!
확실히 rest api uri 규칙을 지키면 간결하고 가독성도 좋은 것 같아요ㅎㅎ 말씀하신대로 다 적용해서 고쳐보도록 하겠습니다!
복수형으로 변경한 커밋 : fa951d0
| @PostMapping("/save") | ||
| public ResponseEntity<CustomerResponse> save(@Validated CustomerCreateRequest customerCreateRequest) { | ||
| CustomerValidator.validateCustomer( | ||
| customerCreateRequest.getCustomerName(), | ||
| customerCreateRequest.getCustomerEmail(), | ||
| customerCreateRequest.getCustomerType() | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
검증 관련한 부분은 이전 PR리뷰를 참고해주세요.
rest api의 uri에는 동사 사용을 지양해주세요.
POST /api/customershttp 메소드와 리소스만으로도 어떤 api인지 바로 알아볼 수 있어야합니다,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
검증 부분은 1차 PR에서 삭제처리하고 dto에서 검증할 수 있도록 처리했습니다!
말씀하신 uri에서 동사를 제외하고 메소드와 리소스만으로도 알 수 있도록 뒤에 /save부분을 삭제처리했습니다!
적용 커밋 : fa951d0
| return new ResponseEntity<>(customerResponse, HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @GetMapping("/find") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기도 ~
GET /api/customersThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/find 삭제처리했습니다!
적용 커밋 : fa951d0
| return ResponseEntity.ok(customerListResponse.getCustomerList()); | ||
| } | ||
|
|
||
| @GetMapping("/find/{id}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기도 ~
GET /api/customers/{id}어떤 api인지 바로 읽히나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return ResponseEntity.ok(customerResponse); | ||
| } | ||
|
|
||
| @GetMapping("/delete/{id}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @GetMapping("/delete/{id}") | |
| @DeleteMapping("/{id}") |
http method 종류에 대해 공부해봅시다!
https://inpa.tistory.com/entry/WEB-%F0%9F%8C%90-HTTP-%EB%A9%94%EC%84%9C%EB%93%9C-%EC%A2%85%EB%A5%98-%ED%86%B5%EC%8B%A0-%EA%B3%BC%EC%A0%95-%F0%9F%92%AF-%EC%B4%9D%EC%A0%95%EB%A6%AC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
예전에 흘려듣기로 Delete와 Put메소드를 사용하면 보안에 좋지 않다라는 말을 들었었는데 찾아보니 제가 잘 안찾아보고 그냥 안썼던거네요ㅠ http method 종류를 직접 적용할 수 있도록 해보겠습니다!!
delete method 적용 커밋 : fa951d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
관련된 내용은 요거같네요
https://velog.io/@awdsza/HTTPMethod-PUTDELETE%EB%B3%B4%EC%95%88-%EC%9C%84%ED%97%98%EC%84%B1
http method의 멱등성에 대해서도 공부해보시길 바랍니다~
| @RestController | ||
| @RequestMapping("/api/voucher") | ||
| @RequiredArgsConstructor | ||
| public class VoucherApiController { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
customer 쪽과 동일하게 리팩토링해주세요~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
말씀하신대로 동일하게 반영했습니다!
|
|
||
| VoucherResponse voucherResponse = voucherService.save(voucherCreateRequest); | ||
|
|
||
| return new ResponseEntity<>(voucherResponse, HttpStatus.CREATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아래처럼 쓸수도 있습니다~~
| return new ResponseEntity<>(voucherResponse, HttpStatus.CREATED); | |
| return ResponseEntity.status(HttpStatus.CREATED).body(voucherResponse); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
창현님은 과제를 빨리 하셨기 때문에...!
리뷰 반영하시면서 추가로 아래와 같은 요구사항도 구현해보시면 좋을것 같습니다~
(여유가 없으시다면 그냥 지나치셔도 됩니다. 어차피 게시판 미션에서 하게됩니다 ㅋㅋ)
- voucher 업데이트 api
- voucher 타입별 조회 api (fixed, rate를 파라미터로 받아서 조회하기)
- voucher 전체 페이징 조회 api
|
미리 경험해보는 것도 좋을 것 같아서 조금 늦더라도 1차 프로젝트 과제 기한 지키면서 조금씩 추가해보겠습니다!! 👍👍 |
mentor-tyler
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수고하셨습니다.
| CustomerValidator.validateCustomer( | ||
| customerCreateRequest.getCustomerName(), | ||
| customerCreateRequest.getCustomerEmail(), | ||
| customerCreateRequest.getCustomerType() | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Custom Validator 를 적용하면 한방에 처리되지 않을까요?
|
|
||
| @Slf4j | ||
| @ControllerAdvice | ||
| // @ControllerAdvice |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇다면 제거?ㅎㅎ
|
|
||
| @ExceptionHandler(NoSuchElementException.class) | ||
| public ResponseEntity<ErrorResponse> handleNoSuchElementException(NoSuchElementException e) { | ||
| log.warn("RestGlobalExceptionHandler - NoSuchElementException 발생, 찾는 데이터가 없음 {}", e.getMessage(), e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| log.warn("RestGlobalExceptionHandler - NoSuchElementException 발생, 찾는 데이터가 없음 {}", e.getMessage(), e); | |
| log.warn("찾는 데이터가 없음", e); |
📌 과제 설명
3주차 미션
3-1
3-2
👩💻 요구 사항과 구현 내용
3-2 JSON을 지원하는 바우처 관리페이지 REST API 구현
요약
Voucher(api)
Customer(api)
RestControllerAdvice
Postman Api Test
✅ PR 포인트 & 궁금한 점