diff --git a/keyword/chapter07/keyword.md b/keyword/chapter07/keyword.md new file mode 100644 index 0000000..800c7d9 --- /dev/null +++ b/keyword/chapter07/keyword.md @@ -0,0 +1,70 @@ +## ๐ŸŽฏํ•ต์‹ฌ ํ‚ค์›Œ๋“œ + +--- + +***1. @RestControllerAdvice*** + +- @RestControllerAdvice๋Š” REST API ์ „์šฉ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ์ „์—ญ์—์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋•๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. ์Šคํ”„๋ง์—์„œ ์˜ˆ์™ธ๋ฅผ ๋‹ค๋ฃจ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ์‹ ์ค‘ @RestControllerAdvice๋Š” API์˜ ์ „๋ฐ˜์ ์ธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ ๊ณณ์—์„œ ๋‹ด๋‹นํ•˜์—ฌ, ์ผ๊ด€๋œ ์‘๋‹ต ๊ตฌ์กฐ๋กœ ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. + +- @RestControllerAdvice์˜ ์ฃผ์š” ๊ธฐ๋Šฅ ๋ฐ ์žฅ์  + - ์ „์—ญ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ: ํŠน์ • ์ปจํŠธ๋กค๋Ÿฌ์— ์ข…์†๋˜์ง€ ์•Š๊ณ , ๋ชจ๋“  @RestController์˜ ์˜ˆ์™ธ๋ฅผ ํฌ๊ด„์ ์œผ๋กœ ๊ด€๋ฆฌํ•œ๋‹ค. + - ์ผ๊ด€๋œ JSON ์‘๋‹ต: ์‘๋‹ต ํ˜•์‹์ด JSON์œผ๋กœ ๊ณ ์ •๋˜๋ฏ€๋กœ RESTful ์„œ๋น„์Šค์— ์ ํ•ฉํ•˜๋ฉฐ, API ์‚ฌ์šฉ์ž์—๊ฒŒ ์ผ๊ด€๋œ ์—๋Ÿฌ ํ˜•์‹์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. + - ํ•ธ๋“ค๋Ÿฌ ๋ฉ”์„œ๋“œ: @ExceptionHandler๋ฅผ ํ†ตํ•ด ์˜ˆ์™ธ ์œ ํ˜•๋ณ„๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜์—ฌ, ์ƒํ™ฉ์— ๋งž๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€์™€ HTTP ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. + +- @RestControllerAdvice ํ™œ์šฉ ์‹œ ์ฃผ์˜์‚ฌํ•ญ + - ํด๋ž˜์Šค ๋ถ„๋ฆฌ: ์˜ˆ์™ธ์˜ ๋ณต์žก์„ฑ์ด ์ปค์งˆ ๊ฒฝ์šฐ, ์—ฌ๋Ÿฌ @RestControllerAdvice ํด๋ž˜์Šค๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ๋ชจ๋“ˆํ™”ํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. + - ์‘๋‹ต ํ˜•์‹ ํ†ต์ผ: ์—๋Ÿฌ ์‘๋‹ต์˜ ํฌ๋งท์„ ์ •ํ•ด ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ผ๊ด€๋œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค. + - ์ตœ์ƒ์œ„ ํ•ธ๋“ค๋Ÿฌ ์„ค์ •: ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ์˜ˆ์™ธ์— ๋Œ€๋น„ํ•ด ์ตœ์ƒ์œ„ ์˜ˆ์™ธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ •์˜ํ•˜์—ฌ, ๋ชจ๋“  ์—๋Ÿฌ๊ฐ€ ์ ์ ˆํ•œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋กœ ์‘๋‹ต๋  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. + + - ExceptionHandler ์‚ฌ์šฉ ์˜ˆ์‹œ + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(CustomException.class) + public ResponseEntity handleCustomException(CustomException ex) { + return buildErrorResponse(ex.getErrorCode()); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleGeneralException(Exception ex) { + return buildErrorResponse(CommonErrorCode.INTERNAL_SERVER_ERROR); + } + + private ResponseEntity buildErrorResponse(ErrorCode errorCode) { + return ResponseEntity.status(errorCode.getHttpStatus()) + .body(new ErrorResponse(errorCode.name(), errorCode.getMessage())); + } +} + + +--- + + +***2. Lombok*** + +- Lombok์€ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ ์š”์†Œ๋ฅผ ์ž๋™ ์ƒ์„ฑํ•˜๋Š” ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ๋‹ค์–‘ํ•œ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ œ๊ณตํ•˜์—ฌ ์ƒ์‚ฐ์„ฑ์„ ๋†’์ด๊ณ  ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค. + +- Lombok์˜ ์ฃผ์š” ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐ ๊ธฐ๋Šฅ + - @Getter, @Setter: ํด๋ž˜์Šค๋‚˜ ํ•„๋“œ์— ์ ์šฉํ•˜์—ฌ getter, setter ๋ฉ”์„œ๋“œ๋ฅผ ์ž๋™ ์ƒ์„ฑํ•œ๋‹ค. + - @AllArgsConstructor / @NoArgsConstructor: ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋‚˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™ ์ƒ์„ฑํ•œ๋‹ค. + - @Builder: ๋นŒ๋” ํŒจํ„ด์„ ์ ์šฉํ•ด ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•œ๋‹ค. + - @ToString: toString() ๋ฉ”์„œ๋“œ๋ฅผ ์ž๋™ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ( ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋‚˜์˜ฌ ๋ฉ”์„ธ์ง€๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ) + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@ToString +public class User { + private Long id; + private String name; + private String email; +} + +- Lombok ์‚ฌ์šฉ ์‹œ ์œ ์˜์  + - ๋””๋ฒ„๊น…์˜ ์–ด๋ ค์›€: ์ž๋™ ์ƒ์„ฑ๋œ ์ฝ”๋“œ๊ฐ€ ๋ณด์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋””๋ฒ„๊น… ์‹œ ์ƒ์„ฑ๋œ ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ ํ๋ฆ„์„ ์ถ”์ ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. + - ์˜์กด์„ฑ: Lombok์€ ์ปดํŒŒ์ผ ํƒ€์ž„์— ๋™์ž‘ํ•˜๋ฏ€๋กœ, ํ”„๋กœ์ ํŠธ์˜ ๋นŒ๋“œ ๋„๊ตฌ์™€์˜ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ์— ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. + + +---