Skip to content

Commit

Permalink
#33 [Add] Api ResponseDTO 예외처리 핸들러 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Jul 19, 2022
1 parent fa11a34 commit a511a0a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ public Map<String, Object> 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);
headers.set("Authorization", "KakaoAK 04940cceefec44d7adb62166b7971cd5");
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();
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> validExceptionNotControlled(ConstraintViolationException cve) {
return ErrorResponse.constraintMsgToResponseEntity(ErrorCode.WRONG_INPUT, cve.getConstraintViolations().iterator().next().getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException mae) {
return ErrorResponse.constraintMsgToResponseEntity
(ErrorCode.WRONG_INPUT, mae.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/mpnp/baechelin/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 결과가 존재하지 않습니다.");

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/mpnp/baechelin/exception/ErrorResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import org.springframework.http.ResponseEntity;

import javax.validation.ConstraintViolationException;
import java.time.LocalDateTime;

@Getter
Expand All @@ -26,4 +27,16 @@ public static ResponseEntity<ErrorResponse> toResponseEntity(ErrorCode errorCode
.build()
);
}

public static ResponseEntity<ErrorResponse> 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()
);
}
}

0 comments on commit a511a0a

Please sign in to comment.