From a511a0af191f10df7c4d28b3f575ecc597f54648 Mon Sep 17 00:00:00 2001 From: JSoi Date: Wed, 20 Jul 2022 00:23:32 +0900 Subject: [PATCH] =?UTF-8?q?#33=20[Add]=20Api=20ResponseDTO=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=ED=95=B8=EB=93=A4=EB=9F=AC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../api/service/LocationService.java | 16 +++++++------- .../api/service/PublicApiService.java | 2 +- .../exception/BeanValidationException.java | 22 +++++++++++++++++++ .../mpnp/baechelin/exception/ErrorCode.java | 2 +- .../baechelin/exception/ErrorResponse.java | 13 +++++++++++ 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/mpnp/baechelin/exception/BeanValidationException.java diff --git a/build.gradle b/build.gradle index 4999310..9ce6b97 100644 --- a/build.gradle +++ b/build.gradle @@ -53,6 +53,7 @@ dependencies { // validation implementation 'org.springframework.boot:spring-boot-starter-validation' annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" + implementation 'org.hibernate.validator:hibernate-validator' //queryDSL implementation "com.querydsl:querydsl-jpa:${queryDslVersion}" implementation "com.querydsl:querydsl-apt:${queryDslVersion}" diff --git a/src/main/java/com/mpnp/baechelin/api/service/LocationService.java b/src/main/java/com/mpnp/baechelin/api/service/LocationService.java index dae3579..931b0b7 100644 --- a/src/main/java/com/mpnp/baechelin/api/service/LocationService.java +++ b/src/main/java/com/mpnp/baechelin/api/service/LocationService.java @@ -106,7 +106,7 @@ public Map convertAddressToGeo(String address) { * @param address 변환할 주소 * @return RestTemplate를 이용해 변환한 위도, 경도 */ - public LocationKeywordSearchForm getLatLngByAddressRest(String address) { + public LocationKeywordSearchForm getLatLngByAddressRest(String address, int page, int size) { HttpHeaders headers = new HttpHeaders(); headers.setAccept(List.of(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_JSON); @@ -114,8 +114,8 @@ public LocationKeywordSearchForm getLatLngByAddressRest(String address) { URI uri = UriComponentsBuilder .fromUriString("https://dapi.kakao.com/v2/local/search/keyword.json") .queryParam("query", address) - .queryParam("page", 1) - .queryParam("size", 1) + .queryParam("page", page) + .queryParam("size", size) .encode() .build() .toUri(); @@ -128,9 +128,9 @@ public LocationKeywordSearchForm getLatLngByAddressRest(String address) { } public LocationKeywordSearchForm getCategoryByLatLngKeywordRest(String lat, String lng, String storeName) { - LocationKeywordSearchForm searchFormResult = getCategoryByCode(lat, lng, storeName, "FD6"); + LocationKeywordSearchForm searchFormResult = getCategoryByCode(lat, lng, storeName, "FD6",1,1); if (searchFormResult == null) { - return getCategoryByCode(lat, lng, storeName, "CE7"); + return getCategoryByCode(lat, lng, storeName, "CE7",1,1); } return searchFormResult; } @@ -142,7 +142,7 @@ public LocationKeywordSearchForm getCategoryByLatLngKeywordRest(String lat, Stri * @param cateCode 카테고리 코드 * @return 위도, 경도, 업장명, 카테고리 코드 조건에 맞는 정보를 리턴 */ - public LocationKeywordSearchForm getCategoryByCode(String lat, String lng, String storeName, String cateCode) { + public LocationKeywordSearchForm getCategoryByCode(String lat, String lng, String storeName, String cateCode, int page, int size) { HttpHeaders headers = new HttpHeaders(); headers.setAccept(List.of(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_JSON); @@ -154,8 +154,8 @@ public LocationKeywordSearchForm getCategoryByCode(String lat, String lng, Strin .queryParam("y", lat) .queryParam("category_group_code", cateCode) .queryParam("radius", 200) - .queryParam("page", 1) - .queryParam("size", 1) + .queryParam("page", page) + .queryParam("size", size) .encode() .build() .toUri(); diff --git a/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java b/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java index 9f1875c..f2b820c 100644 --- a/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java +++ b/src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java @@ -118,7 +118,7 @@ private void setInfos(PublicApiResponseDto publicApiResponseDto) { private boolean setRowLngLat(PublicApiResponseDto.Row row) throws JsonProcessingException { - LocationKeywordSearchForm latLngSearchForm = locationService.getLatLngByAddressRest(row.getADDR()); + LocationKeywordSearchForm latLngSearchForm = locationService.getLatLngByAddressRest(row.getADDR(),1,1); // LocationKeywordSearchForm latLngSearchForm = locationService.giveLatLngByAddress(row.getADDR()); if (latLngSearchForm == null) return false; LocationKeywordSearchForm.Documents latLngDoc = Arrays.stream(latLngSearchForm.getDocuments()).findFirst().orElse(null); diff --git a/src/main/java/com/mpnp/baechelin/exception/BeanValidationException.java b/src/main/java/com/mpnp/baechelin/exception/BeanValidationException.java new file mode 100644 index 0000000..770f895 --- /dev/null +++ b/src/main/java/com/mpnp/baechelin/exception/BeanValidationException.java @@ -0,0 +1,22 @@ +package com.mpnp.baechelin.exception; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import javax.validation.ConstraintViolationException; + +@RestControllerAdvice +public class BeanValidationException { + // Bean Validation ExceptionHandler + @ExceptionHandler(ConstraintViolationException.class) + public ResponseEntity validExceptionNotControlled(ConstraintViolationException cve) { + return ErrorResponse.constraintMsgToResponseEntity(ErrorCode.WRONG_INPUT, cve.getConstraintViolations().iterator().next().getMessage()); + } + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException mae) { + return ErrorResponse.constraintMsgToResponseEntity + (ErrorCode.WRONG_INPUT, mae.getBindingResult().getAllErrors().get(0).getDefaultMessage()); + } +} diff --git a/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java b/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java index db5e5f8..cf91f67 100644 --- a/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java +++ b/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java @@ -17,7 +17,7 @@ public enum ErrorCode { ACCESS_DENIED(401, "E-ACD401","접근이 거부되었습니다."), TOKEN_NOT_EXIST(401, "E-TNE401", "토큰이 존재하지 않습니다."), ALREADY_LOGIN_ACCOUNT(400, "E-ALA400","다른 계정으로 로그인 되었습니다."), - + WRONG_INPUT(400, "E_WI400", "입력 값을 확인해주세요"), API_LOAD_FAILURE(500, "E-ALF500", "API 로딩에 실패하였습니다."), API_NO_RESULT(500, "E-ALF500", "API 결과가 존재하지 않습니다."); diff --git a/src/main/java/com/mpnp/baechelin/exception/ErrorResponse.java b/src/main/java/com/mpnp/baechelin/exception/ErrorResponse.java index 265af2e..88b00df 100644 --- a/src/main/java/com/mpnp/baechelin/exception/ErrorResponse.java +++ b/src/main/java/com/mpnp/baechelin/exception/ErrorResponse.java @@ -4,6 +4,7 @@ import lombok.Getter; import org.springframework.http.ResponseEntity; +import javax.validation.ConstraintViolationException; import java.time.LocalDateTime; @Getter @@ -26,4 +27,16 @@ public static ResponseEntity toResponseEntity(ErrorCode errorCode .build() ); } + + public static ResponseEntity constraintMsgToResponseEntity(ErrorCode errorCode, String msg) { + return ResponseEntity + .status(errorCode.getStatus()) + .body(ErrorResponse.builder() + .status(errorCode.getStatus()) + .code(errorCode.getCode()) + .error(errorCode.name()) + .message(msg) + .build() + ); + } }