#26 pharmacy: kakao api - local address search

This commit is contained in:
haerong22
2022-12-12 00:43:53 +09:00
parent eb2c1a1967
commit b972aefc79
7 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
package com.example.road.api.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class DocumentDto {
@JsonProperty("address_name")
private String addressName;
@JsonProperty("y")
private double latitude;
@JsonProperty("x")
private double longitude;
}

View File

@@ -0,0 +1,20 @@
package com.example.road.api.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.List;
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class KakaoApiResponseDto {
@JsonProperty("meta")
private MetaDto metaDto;
@JsonProperty("documents")
private List<DocumentDto> documentDtoList;
}

View File

@@ -0,0 +1,15 @@
package com.example.road.api.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class MetaDto {
@JsonProperty("total_count")
private Integer totalCount;
}

View File

@@ -0,0 +1,41 @@
package com.example.road.api.service;
import com.example.road.api.dto.KakaoApiResponseDto;
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.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
@Slf4j
@Service
@RequiredArgsConstructor
public class KakaoAddressSearchService {
private final RestTemplate restTemplate;
private final KakaoUriBuilderService kakaoUriBuilderService;
@Value("${kakao.rest.api.key}")
private String kakaoRestApiKey;
public KakaoApiResponseDto requestAddressSearch(String address) {
if (ObjectUtils.isEmpty(address)) return null;
URI uri = kakaoUriBuilderService.builderUriByAddressSearch(address);
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, "KakaoAK " + kakaoRestApiKey);
HttpEntity httpEntity = new HttpEntity<>(headers);
// kakao api 호출
return restTemplate.exchange(uri, HttpMethod.GET, httpEntity, KakaoApiResponseDto.class).getBody();
}
}

View File

@@ -0,0 +1,25 @@
package com.example.road.api.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
@Slf4j
@Service
public class KakaoUriBuilderService {
private static final String KAKAO_LOCAL_SEARCH_ADDRESS_URL = "https://dapi.kakao.com/v2/local/search/address.json";
public URI builderUriByAddressSearch(String address) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(KAKAO_LOCAL_SEARCH_ADDRESS_URL);
uriBuilder.queryParam("query", address);
URI uri = uriBuilder.build().encode().toUri();
log.info("[KakaoUriBuilderService builderUriByAddressSearch] address: {}, uri: {}", address, uri);
return uri;
}
}

View File

@@ -0,0 +1,14 @@
package com.example.road.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

View File

@@ -13,6 +13,11 @@ spring:
activate:
on-profile: common
kakao:
rest:
api:
key: ${KAKAO_REST_API_KEY}
---
spring:
config: