From 12c72aa7757ead2311ad88ee2ab076ccc5492d06 Mon Sep 17 00:00:00 2001 From: david-parkk Date: Sat, 20 Jul 2024 23:36:01 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20valid=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 ++ .../ourMenu/backend/domain/test/api/TestController.java | 6 ++++-- .../backend/domain/test/api/request/SaveJdbcRequest.java | 7 +++++++ .../backend/global/exception/GlobalExceptionHandler.java | 9 ++++++++- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/ourMenu/backend/domain/test/api/request/SaveJdbcRequest.java diff --git a/build.gradle b/build.gradle index 5c13c4e1..1bd29b36 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,8 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' //swagger implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2' + //validation + implementation 'org.springframework.boot:spring-boot-starter-validation' } tasks.named('test') { diff --git a/src/main/java/com/ourMenu/backend/domain/test/api/TestController.java b/src/main/java/com/ourMenu/backend/domain/test/api/TestController.java index 71522ff1..d8254d1f 100644 --- a/src/main/java/com/ourMenu/backend/domain/test/api/TestController.java +++ b/src/main/java/com/ourMenu/backend/domain/test/api/TestController.java @@ -1,6 +1,7 @@ package com.ourMenu.backend.domain.test.api; import com.ourMenu.backend.domain.test.api.request.SaveEntityRequest; +import com.ourMenu.backend.domain.test.api.request.SaveJdbcRequest; import com.ourMenu.backend.domain.test.api.response.FindEntityByIdResponse; import com.ourMenu.backend.domain.test.api.response.FindJdbcByIdResponse; import com.ourMenu.backend.domain.test.api.response.SaveEntityResponse; @@ -10,6 +11,7 @@ import com.ourMenu.backend.global.common.ApiResponse; import com.ourMenu.backend.global.util.ApiUtils; import io.swagger.v3.oas.annotations.Operation; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -53,8 +55,8 @@ public ApiResponse findById(@PathVariable("SaveEntityId" } @PostMapping("/jdbc/save") - public void save(@RequestBody String name){ - testService.saveJdbcEntity(name); + public void save(@RequestBody @Valid SaveJdbcRequest saveJdbcRequest){ + testService.saveJdbcEntity(saveJdbcRequest.name()); } @GetMapping("/jdbc/{SaveEntityId}") diff --git a/src/main/java/com/ourMenu/backend/domain/test/api/request/SaveJdbcRequest.java b/src/main/java/com/ourMenu/backend/domain/test/api/request/SaveJdbcRequest.java new file mode 100644 index 00000000..f67ae923 --- /dev/null +++ b/src/main/java/com/ourMenu/backend/domain/test/api/request/SaveJdbcRequest.java @@ -0,0 +1,7 @@ +package com.ourMenu.backend.domain.test.api.request; + +import jakarta.validation.constraints.Size; + +public record SaveJdbcRequest( + @Size(max = 6, message = "이름은 6자를 이하만 가능합니다") String name) { +} diff --git a/src/main/java/com/ourMenu/backend/global/exception/GlobalExceptionHandler.java b/src/main/java/com/ourMenu/backend/global/exception/GlobalExceptionHandler.java index bea49ed3..6abf90ea 100644 --- a/src/main/java/com/ourMenu/backend/global/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/ourMenu/backend/global/exception/GlobalExceptionHandler.java @@ -3,6 +3,9 @@ import com.ourMenu.backend.global.util.ApiUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -10,10 +13,14 @@ @RestControllerAdvice public class GlobalExceptionHandler { - @ExceptionHandler(Exception.class) + @ExceptionHandler(RuntimeException.class) private ResponseEntity handleException(Exception e) { return handleException(e, ErrorCode.INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR.getMessage()); } + @ExceptionHandler(MethodArgumentNotValidException.class) + private ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e){ + return handleException(e,ErrorCode.INTERNAL_SERVER_ERROR, e.getBindingResult().getFieldError().getDefaultMessage()); + } private ResponseEntity handleException(Exception e, ErrorCode errorCode, String message) { return ApiUtils.error(ErrorResponse.of(errorCode, message));