diff --git a/src/main/java/com/haemil/backend/alert/controller/AlertController.java b/src/main/java/com/haemil/backend/alert/controller/AlertController.java index 262b7a1..08d80ae 100644 --- a/src/main/java/com/haemil/backend/alert/controller/AlertController.java +++ b/src/main/java/com/haemil/backend/alert/controller/AlertController.java @@ -34,11 +34,11 @@ public ResponseEntity sendGetRequest() { try { String jsonString = alertService.getAlertInfo(getApiDto); - log.debug("jsonString: "+jsonString); +// log.debug("jsonString: "+jsonString); alertService.isJson(jsonString); - List infoList = alertService.ParsingJson(jsonString); - return new BaseResponse<>(infoList).convert(); + ApiInfoDto apiInfoDto = alertService.ParsingJson(jsonString); + return new BaseResponse<>(apiInfoDto).convert(); } catch (BaseException e){ // 실패시 custom한 status로 code 헤더 설정, body로 메세지 반환 return new BaseResponse<>(e.getStatus()).convert(); diff --git a/src/main/java/com/haemil/backend/alert/service/AlertService.java b/src/main/java/com/haemil/backend/alert/service/AlertService.java index e8913cc..0291d6c 100644 --- a/src/main/java/com/haemil/backend/alert/service/AlertService.java +++ b/src/main/java/com/haemil/backend/alert/service/AlertService.java @@ -11,11 +11,15 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import org.springframework.web.client.UnknownContentTypeException; import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -38,28 +42,50 @@ public String getAlertInfo(GetApiDto reqGetApiDto) throws BaseException { String pageNo = reqGetApiDto.getPageNo(); String numOfRows = reqGetApiDto.getNumOfRows(); - log.debug("serviceKey: " + serviceKey); - StringBuilder urlBuilder = new StringBuilder(apiUrl); urlBuilder.append("?" + URLEncoder.encode("ServiceKey", "UTF-8") + "=" + serviceKey); urlBuilder.append("&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode(type, "UTF-8")); urlBuilder.append("&" + URLEncoder.encode("pageNo", "UTF-8") + "=" + URLEncoder.encode(pageNo, "UTF-8")); urlBuilder.append("&" + URLEncoder.encode("numOfRows", "UTF-8") + "=" + URLEncoder.encode(numOfRows, "UTF-8")); - ResponseEntity response = restTemplate.getForEntity(urlBuilder.toString(), String.class); +// log.debug("urlBuilder: {}", urlBuilder); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("Accept", "*/*;q=0.9"); // HTTP_ERROR 방지 + HttpEntity httpRequest = new HttpEntity<>(null, headers); +// log.debug("httpRequest = {}", httpRequest); + + RestTemplate restTemplate = new RestTemplate(); +// log.debug("restTemplate = {}", restTemplate); - responseBody = response.getBody(); + HttpStatus httpStatus = null; + ResponseEntity httpResponse = null; + + URI uri = new URI(urlBuilder.toString()); // service key is not registered 오류 방지 +// log.debug("uri = {}", uri); + httpResponse = restTemplate.exchange(uri, HttpMethod.GET, httpRequest, new ParameterizedTypeReference(){}); +// log.debug("httpResponse = {}", httpResponse); + + if (httpResponse != null && httpResponse.getBody() != null) { + responseBody = httpResponse.getBody(); + } +// log.debug("responseBody = {}",responseBody); } catch (UnsupportedEncodingException e) { // 에러가 발생했을 때 예외 status 명시 log.debug("UnsupportedEncodingException 발생 "); throw new BaseException(ResponseStatus.UNSUPPORTED_ENCODING); + } catch (URISyntaxException e) { + log.debug("URISyntaxException 발생 "); + throw new BaseException(ResponseStatus.UNSUPPORTED_ENCODING); } + return responseBody; } - public List ParsingJson(String responseBody) throws BaseException { + public ApiInfoDto ParsingJson(String responseBody) throws BaseException { + ApiInfoDto apiInfoDto; - List apiInfoDtoList; try { List alertApiList = new ArrayList<>(); ObjectMapper objectMapper = new ObjectMapper(); @@ -72,8 +98,8 @@ public List ParsingJson(String responseBody) throws BaseException { String msg = nextNode.get("msg").asText(); String location = nextNode.get("location_name").asText(); // 예시로 location_name을 사용하여 location 값을 가져옴 - log.debug("msg: " + msg); - log.debug("location: " + location); +// log.debug("msg: " + msg); +// log.debug("location: " + location); AlertApi alertApi = AlertApi.builder() .msg(msg) @@ -83,19 +109,18 @@ public List ParsingJson(String responseBody) throws BaseException { alertApiList.add(alertApi); // 변환된 데이터를 ApiInfoDto 형태로 리스트로 반환 - apiInfoDtoList = new ArrayList<>(); + apiInfoDto = new ApiInfoDto(); + for (AlertApi a : alertApiList) { - ApiInfoDto apiInfoDto = new ApiInfoDto(); apiInfoDto.setMsg(a.getMsg()); apiInfoDto.setLocation(a.getLocation()); - apiInfoDtoList.add(apiInfoDto); } - log.debug("apiInfoDtoList: " + apiInfoDtoList); +// log.debug("apiInfoDto: " + apiInfoDto); } catch (JsonProcessingException e) { // 에러가 발생했을 때 예외 status 명시 throw new BaseException(ResponseStatus.CANNOT_CONVERT_JSON); } - return apiInfoDtoList; + return apiInfoDto; } public boolean isJson(String xmlString) throws BaseException { diff --git a/src/main/java/com/haemil/backend/global/config/ResponseStatus.java b/src/main/java/com/haemil/backend/global/config/ResponseStatus.java index d991189..b7f67a0 100644 --- a/src/main/java/com/haemil/backend/global/config/ResponseStatus.java +++ b/src/main/java/com/haemil/backend/global/config/ResponseStatus.java @@ -26,6 +26,7 @@ public enum ResponseStatus { // --- 55x Custom Error -- CANNOT_CONVERT_JSON(false, 550, "JSON 문자열로 변경할 수 없습니다."), UNSUPPORTED_ENCODING(false, 551, "지원되지 않는 인코딩 형식입니다."), + URI_SYNT(false, 551, "URISyntaxException이 발생했습니다."), INVALID_XML_FORMAT(false, 552, "SERVICE ERROR가 발생했습니다."), UNKNOWN_ADDR(false, 553, "알 수 없는 주소를 입력받았습니다.");