Skip to content

Commit

Permalink
feat: valid 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
david-parkk committed Jul 20, 2024
1 parent 6eaac62 commit 12c72aa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;

Expand Down Expand Up @@ -53,8 +55,8 @@ public ApiResponse<FindEntityByIdResponse> 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}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
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;

@Slf4j
@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));
Expand Down

0 comments on commit 12c72aa

Please sign in to comment.