Memorization
20220708 TIL
코드파고
2022. 7. 9. 04:02
WebClient를 버려야 하는걸까? 😢
러닝커브가 너무 높다 허허;;
그래서 구글링하다가 찾은
Lazy Subscribe를 통한 Stream 또는 Iterable 로 변환 시킬 수 있는 Flux.toStream() , Flux.toIterable() 함수를 이용했다
내일은 위의 방식으로 구현한 WebClient와 RestTemplate을 비교해보고자 한다.
둘 중 효율이 높은 쪽을 선택할 것 같다😔
WebClient로 호출한 REST API
public LocationKeywordSearchForm giveLatLngByAddressRest(String address) throws JsonProcessingException {
WebClient client = WebClient.builder()
.baseUrl("https://dapi.kakao.com/v2/local/search/keyword.json")
.defaultUriVariables(Collections.singletonMap("url", "https://dapi.kakao.com/v2/local/search/keyword.json"))
.clientConnector(new ReactorClientHttpConnector(httpConfig.httpClient())) // 위의 타임아웃 적용
.build();
return client.get().uri(uriBuilder
-> uriBuilder.queryParam("query", address)
.queryParam("page", 1)
.queryParam("size", 1)
.build())
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", "KakaoAK XXX")
.retrieve().bodyToMono(LocationKeywordSearchForm.class).log().flux()
.toStream()
.findFirst()
.orElse(null);
}
RestTemplate로 호출한 REST API
public LocationKeywordSearchForm giveLatLngByAddressRest(String address) throws JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "KakaoAK XXX");
URI uri = UriComponentsBuilder
.fromUriString("https://dapi.kakao.com/v2/local/search/keyword.json")
.queryParam("query", address)
.queryParam("page",1)
.queryParam("size",1)
.encode()
.build()
.toUri();
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<LocationKeywordSearchForm> resultRe = restTemplate.exchange(
uri,HttpMethod.GET,new HttpEntity<>(headers),LocationKeywordSearchForm.class
);
return resultRe.getBody();
}