주소창 백엔드단에서 api 호출하도록 변경함
parent
b6cf830661
commit
870318ace5
@ -0,0 +1,40 @@
|
||||
package go.kr.project.common.controller;
|
||||
|
||||
import go.kr.project.common.service.AddressService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 주소 검색 관련 컨트롤러
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/common/address")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "주소 검색", description = "외부 주소 API 연동 관련")
|
||||
public class AddressController {
|
||||
|
||||
private final AddressService addressService;
|
||||
|
||||
/**
|
||||
* 외부 주소 API를 호출하여 검색 결과를 반환합니다.
|
||||
* @param keyword 검색어
|
||||
* @param currentPage 현재 페이지
|
||||
* @param countPerPage 페이지당 결과 수
|
||||
* @return API 응답 결과 (JSON 문자열)
|
||||
*/
|
||||
@GetMapping(value = "/search.ajax", produces = "application/json; charset=UTF-8")
|
||||
@Operation(summary = "주소 검색", description = "외부 도로명주소 API를 호출하여 결과를 반환합니다.")
|
||||
public String searchAddress(
|
||||
@Parameter(description = "검색어") @RequestParam("keyword") String keyword,
|
||||
@Parameter(description = "현재 페이지") @RequestParam("currentPage") int currentPage,
|
||||
@Parameter(description = "페이지당 결과 수") @RequestParam("countPerPage") int countPerPage) {
|
||||
|
||||
return addressService.searchAddress(keyword, currentPage, countPerPage);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package go.kr.project.common.service;
|
||||
|
||||
/**
|
||||
* 주소 검색 서비스 인터페이스
|
||||
*/
|
||||
public interface AddressService {
|
||||
|
||||
/**
|
||||
* 외부 주소 API를 호출하여 주소를 검색합니다.
|
||||
*
|
||||
* @param keyword 검색어
|
||||
* @param currentPage 현재 페이지
|
||||
* @param countPerPage 페이지당 결과 수
|
||||
* @return API 응답 결과 (JSON 문자열)
|
||||
*/
|
||||
String searchAddress(String keyword, int currentPage, int countPerPage);
|
||||
}
|
||||
@ -0,0 +1,73 @@
|
||||
package go.kr.project.common.service.impl;
|
||||
|
||||
import go.kr.project.common.service.AddressService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 주소 검색 서비스 구현 클래스
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AddressServiceImpl implements AddressService {
|
||||
|
||||
// 중요로직: application.yml에서 API 키 주입
|
||||
@Value("${juso.api.key}")
|
||||
private String confmKey;
|
||||
|
||||
@Value("${juso.api.url}")
|
||||
private String API_URL;
|
||||
|
||||
@Override
|
||||
public String searchAddress(String keyword, int currentPage, int countPerPage) {
|
||||
log.debug("주소 API 호출: keyword={}, currentPage={}, countPerPage={}", keyword, currentPage, countPerPage);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
// HTTP 요청 헤더 설정
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
HttpEntity<String> entity = new HttpEntity<>(headers);
|
||||
|
||||
// URI 및 파라미터 설정
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(API_URL)
|
||||
.queryParam("confmKey", confmKey)
|
||||
.queryParam("currentPage", currentPage)
|
||||
.queryParam("countPerPage", countPerPage)
|
||||
.queryParam("keyword", keyword)
|
||||
.queryParam("resultType", "json");
|
||||
|
||||
try {
|
||||
// 중요로직: 외부 API 호출
|
||||
// 주소 API가 응답의 Content-Type을 text/html로 반환하는 경우가 있어, String으로 받은 후 그대로 전달
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
builder.encode(StandardCharsets.UTF_8).build().toUri(),
|
||||
HttpMethod.GET,
|
||||
entity,
|
||||
String.class);
|
||||
|
||||
return response.getBody();
|
||||
|
||||
} catch (HttpClientErrorException e) {
|
||||
log.error("주소 API 호출 오류: {} - {}", e.getStatusCode(), e.getResponseBodyAsString());
|
||||
// 클라이언트에 전달할 에러 메시지 포맷 생성
|
||||
return String.format("{\"results\":{\"common\":{\"errorCode\":\"%s\",\"errorMessage\":\"%s\"}}}",
|
||||
e.getStatusCode().value(),
|
||||
"주소 API 서버로부터 오류 응답을 받았습니다: " + e.getStatusCode().getReasonPhrase());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue