-
Notifications
You must be signed in to change notification settings - Fork 117
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
전남대 BE_안원모 2주차 과제 (1단계) #251
Conversation
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 을 올리기 전에
잘 작동하는지 한 번 확인해보시면 좋을 것 같아요.
컴파일이 잘 되는지, 테스트가 존재한다면 모두 잘 통과가 되는지,
그리고 직접 만든 기능이 제대로 작동하는지 확인해보는건 리뷰를 받기 전 필수 작업이라고 생각합니다.
다른 스텝을 진행해야하니 approve 하겠습니다.
추가 구현을 하면서 해당 PR 에 있는 코멘트 한 번 확인해주세요.
그리고 제가 남긴 질문에 대한 답변도 생각해보시고 코멘트로 달아주시면 좋을 것 같아요!
ex.getBindingResult().getFieldErrors().forEach(error -> errors.put(error.getField(), error.getDefaultMessage())); | ||
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); |
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.
forEach
문과 기본적인 for 문은 어떤 차이점이 있나요?
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.
for와 foreach의 차이
(1) 동기(sync), 비동기(async)의 차이
for는 동기방식이기 때문에 for문 안에 오류가 나면 에러 위치 이후의 이벤트들은 동작하지 않고 멈춰버립니다.
foreach는 ES6 문법으로 콜백함수를 뿌립니다. 비동기 방식으로 진행되기 때문에 foreach문 안에 에러가 발생하더라도 멈추지 않고 동작한다. 대신 원하는 순서와는 다르게 프로그램이 동작할 수 있습다.
(2)성능 차이
foreach문은 "향상된 for문"이라 칭하기도 합니다. 가변적인 배열이나 리스트 크기를 구할 필요가 없어 복잡한 반복문에 적합하며, 인덱스를 생성하여 접근하는 for문보다 수행속도가 더 빠릅니다(내장함수 이기에)
foreach문의 단점으로는
(1) 반복문 내에서 배열이나 리스트 값을 변경하거나 추가할 수 없다
읽기 전용으로 불러오기 때문에 데이터를 수정하는 행위가 불가능하다.
(2) 배열을 역순으로 탐색할 수 없다
가 있습니다.
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.
해당 내용은 블로그...나 chatgpt 같은 곳에서 가져온 것일까요?
물론 그런 곳에서 내용을 접하고 학습하실 수는 있지만 앞으로는 본인의 말로 바꿔서 써주세요.
만약 본인이 직접 쓴 거라면 조금만 신경써서 써주시면 좋을 것 같습니다.
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.
덤으로 ES6 가 무엇인지 확인해보고, 해당 답변과 제 질문이 어떤 관련이 있는지도 고민해보시면 좋을 것 같네요.
@ControllerAdvice | ||
@RestController | ||
public class GlobalExceptionHandler { |
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.
@RestController
는 GlobalExceptionHandler 에 어떤 이유로 붙여주셨나요?
@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.
<@RestController
= @Controller
+ @ResponseBody
>
RestController : 'REST'를 위한 전용 컨트롤러로, RESTFul한 웹 서비스를 구축하기 위해 사용되었습니다. Response Body가 생성되는 방식에서 @Controller
어노테이션과 차이가 있습니다. JSON 형태의 객체 데이터로 반환하여 작동하기에 사용되었습니다.
추가적으로 @RestControllerAdvice
에 대해 학습해 보았습니다.
@RestControllerAdvice
는 @ControllerAdvice
와 @ResponseBody
를 합쳐놓은 어노테이션입니다.
@ControllerAdvice
와 동일한 역할을 수행하고, 추가적으로 @ResponseBody
를 통해 객체를 리턴할 수도 있습니다.
@RestController
는 @ControllerAdvice
와 함께 사용된 경우 @RestController
가 반드시 필요하지는 않습니다. 대신 @ResponseBody
를 사용할 수 있습니다. (앞서 살펴본 RestController내용 때문)
즉, @ControllerAdvice
와 @RestController
를 함께 사용할 필요가 없고. @ControllerAdvice
를 쓴다면
@ControllerAdvice
와 @ResponseBody
를 사용하면 동일한 기능을 구현할 수 있습니다
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
를 쓴다면@ControllerAdvice
와@ResponseBody
를 사용하면 동일한 기능을 구현할 수 있습니다
이 부분 잘 읽어보시면 조금 이상한 것 같아요.
뭔가 하나 빠진 느낌?
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<String> handleGeneralException(Exception ex) { | ||
return new ResponseEntity<>("An error occurred: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); |
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.
exception handler 에서 ResponseEntity.badRequest()
와 ResponseEntity.internalServerError()
를 활용해 볼 수 있을 것 같아요!
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
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getFieldErrors().forEach(error -> errors.put(error.getField(), error.getDefaultMessage()));
return ResponseEntity.badRequest().body(errors);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleGeneralException(Exception ex) {
return ResponseEntity.internalServerError().body("An error occurred: " + ex.getMessage());
}
}` 로 개선하였습니다.
private String imageUrl; | ||
private String description; | ||
private final Long id; | ||
@NotBlank(message = "상품 이름은 필수 입력 항목입니다.") |
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.
@NotBlank
, @NotNull
, 그리고 @NotEmpty
라는 다양한 어노테이션이 존재하는데요,
@NotBlank
를 여기서 사용해주신 이유가 있나요?
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.
상품 이름은 필수 입력 항목 이기에
값이 null이 아니고, 빈 문자열이 아니며, 공백 문자도 유효하지 않도록 하기위해 NotEmpty
,NotNull
보다 좀 더 강력한 검증을 위해 사용 되었습니다.
public Product() { | ||
this.id = null; | ||
this.name = null; | ||
this.price = null; | ||
this.imageUrl = null; | ||
this.description = null; | ||
} |
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.
상품에는 (이름 가격) 등의 필수 입력 요소들이 존재하고 (imgurl이나 description)같은 선택적인 요소들이 존재합니다.
선택적인 요소들의 입력 내용 부재시에도 상품이 추가될 수 있도록 하기위해 기본 생성자를 생성하였습니다.
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.
사용하지 않으면 지워볼수도 있을 것 같아요.
private String description; | ||
private final Long id; | ||
@NotBlank(message = "상품 이름은 필수 입력 항목입니다.") | ||
@Size(max = 15, message = "상품 이름은 최대 15자까지 입력할 수 있습니다.") |
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.
저는 postman 에서 실행을 해서 작동 하는지 확인해 보았습니다. (http://localhost:8080/api/products)
- 모든 제품 조회: GET /api/products
- 제품 생성: POST /api/products Body (JSON):
{ "name": "New Product", "price": 15.99, ,"imageUrl":"http://example.com/product1.jpg","description": "Description for new product" }- 제품 수정: PUT /api/products/{id} Body (JSON): { "name": "Updated Product", "price": 18.99, "description": "Updated description" }
- 제품 삭제: DELETE /api/products/{id}
형식을따라 postman에서 수행했을땐 특수기호, 글자수,카카오 에 해당되는 내용은 상품이 등록되지 않았습니다!
피케이님이 확인하신 방법은 webcontroller에서 확인을 하신 것 같습니다.
제가 이해한 관리자 페이지 구현은 webcontroller로 구현된 관리자 화면으로, 관리자는 상품 등록에 있어서 제한조건을 받지 않는다고 생각했어서 제약조건을 webcontroller에는 구현하지 않았습니다.
md와 협의후에는 관리자가 관리자 화면을 통해서 '카카오' 라는 단어가 사용될수 있도록 하게끔 하고, 글자수,특수기호의 제한도 받지 않아도 된다고 생각해서 webcontroller에서는 제한을 두지 않았습니다.
문제요구사항을 제가 잘못 이해했을까요?
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.
그렇군요 🤔
괜찮을 것 같습니다.
하지만 적어도 가격이 양수인 것, 이름 길이에 대한 검증 등
관리자도 실수 할 수 있는 부분이나 문제를 발생시킬 수 있는 부분은 관리자 api 에서도 다루는게 좋지 않을까 합니다.
'카카오' 라는 단어는 얘기해주신 것처럼 어떤 api 인지, 아니면 요청한 사람이 누구인지에 따라 다른 처리를 하도록 할 수 있겠네요!
private final Long id; | ||
@NotBlank(message = "상품 이름은 필수 입력 항목입니다.") | ||
@Size(max = 15, message = "상품 이름은 최대 15자까지 입력할 수 있습니다.") | ||
@Pattern(regexp = "^[\\p{L}0-9 ()\\[\\]+\\-&/_]+$", message = "상품 이름에는 특수 문자 ( ), [ ], +, -, &, /, _ 만 사용할 수 있습니다.") |
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.
상품 이름에는 특수 문자 ( ), [ ], +, -, &, /, _ 만 사용할 수 있습니다
->"상품 이름에 사용 가능한 특수문자는 ( ), [ ], +, -, &, /, _ 입니다." 로 수정하겠습니다.
해당 내용도 postman에서 확인해 보았습니다 !
@NotBlank(message = "상품 이름은 필수 입력 항목입니다.") | ||
@Size(max = 15, message = "상품 이름은 최대 15자까지 입력할 수 있습니다.") | ||
@Pattern(regexp = "^[\\p{L}0-9 ()\\[\\]+\\-&/_]+$", message = "상품 이름에는 특수 문자 ( ), [ ], +, -, &, /, _ 만 사용할 수 있습니다.") | ||
@Pattern(regexp = "^(?!.*카카오|.*\\p{L}.*카카오.*).*$", message = "상품 이름에 '카카오'를 사용할 수 없습니다.") |
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.
kakao, Kakao, KAKAO 와 같은 표현도 막아보면 좋을 것 같네요!
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.
수정해서 반영하겠습니다.
@@ -17,6 +17,9 @@ <h1>상품 관리 페이지</h1> | |||
<label for="imageUrl">이미지 URL :</label> | |||
<input type="text" id="imageUrl" name="imageUrl"> | |||
|
|||
<label for="description">상품 설명:</label> | |||
<input type="text" id="description" name="description"><br><br> | |||
|
|||
<button type="submit">상품 추가</button> | |||
</form> | |||
|
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.
이전 단계에서 제가 남겼던 질문에 대한 답변도 한 번 생각해보시면 학습에 도움이 될 것 같아요.
그리고 이전 단계 merge 도 하면 좋을 것 같으니 pr 을 수정해주세요!
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.
git 사용법이 아직 익숙하지 않아 1주차 과제 내용을 아래와 같이 merge요청을 드린다고 드렸는데 잘 안된것 같습니다.
kakao-tech-campus-2nd-step2/spring-gift-product#219
kakao-tech-campus-2nd-step2/spring-gift-product#220
kakao-tech-campus-2nd-step2/spring-gift-product#221 어떻게 해야할까요?
step0 반영하여 작성하였고 step1 유효성 검사 및 예외 처리 수행하였습니다.