open api 사용방법 찾아보기
webClient
= Spring WebFlux 라이브러리에 속하는 클라이언트
= HTTP 클라이언트(HTTP 프로토콜을 이용하여 서버와 통신하는 것을 의미, 서버에 API 요청을 보내는 주체) 라이브러리
처리속도가 빠르고 비동기 처리방식을 사용하기 때문에 대용량 처리를 할 때 용이하다
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
}
package com.webclient.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class WebClientServiceImpl {
public void get() {
String code = "myCode";
// webClient 기본 설정
WebClient webClient =
WebClient
.builder()
.baseUrl("http://localhost:8080")
.build();
// api 요청
Map<String, Object> response =
webClient
.get()
.uri(uriBuilder ->
uriBuilder
.path("/api/get")
.queryParam("code", code)
.build())
.retrieve()
.bodyToMono(Map.class)
.block();
// 결과 확인
log.info(response.toString());
}
}
테스트코드
package com.webclient;
import com.webclient.service.WebClientServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WebClientApplicationTests {
@Autowired
private WebClientServiceImpl webClientService;
@Test
void get() {
webClientService.get();
}
}
이때 console
INFO 18660 --- [ Test worker] c.w.service.WebClientServiceImpl : {code=myCode, message=Success}
package com.webclient.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class WebClientServiceImpl {
public void post() {
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("name", "seorin");
bodyMap.put("age", 123);
// webClient 기본 설정
WebClient webClient =
WebClient
.builder()
.baseUrl("http://localhost:8080")
.build();
// api 요청
Map<String, Object> response =
webClient
.post()
.uri("/api/post")
.bodyValue(bodyMap)
.retrieve()
.bodyToMono(Map.class)
.block();
// 결과 확인
log.info(response.toString());
}
}
테스트코드
package com.webclient;
import com.webclient.service.WebClientServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WebClientApplicationTests {
@Autowired
private WebClientServiceImpl webClientService;
@Test
void post() {
webClientService.post();
}
}
이때 console
INFO 15788 --- [ Test worker] c.w.service.WebClientServiceImpl : {name=seorin, message=Success , age=123
package com.webclient.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class WebClientServiceImpl {
public void defaultValue() {
String code = "myCode";
// webClient 기본 설정
WebClient webClient =
WebClient
.builder()
.baseUrl("http://localhost:8080")
**.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultCookie("cookie", "cookieValue")**
.build();
// api 요청 - 1
webClient
.get()
.uri(uriBuilder ->
uriBuilder
.path("/api/get")
.queryParam("code", code)
.build())
.retrieve()
.bodyToMono(Map.class)
.block();
// api 요청 - 2
webClient
.get()
.uri(uriBuilder ->
uriBuilder
.path("/api/get")
.queryParam("code", code)
.build())
.retrieve()
.bodyToMono(Map.class)
.block();
}
}
.retrieve() 가 붙어있는데,retrieve의 return 값 3가지(response값 변환)
이런 상황을 위해 webClient에서는 Mono.zip이라는 메소드를 제공한다
package com.webclient.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Slf4j
public class WebClientServiceImpl {
public void getMultiple() {
String code = "myCode";
// webClient 기본 설정
WebClient webClient =
WebClient
.builder()
.baseUrl("http://localhost:8080")
.build();
// api 요청 - 1
Mono<Map> responseMono1 =
webClient
.get()
.uri(uriBuilder ->
uriBuilder
.path("/api/get")
.queryParam("code", code)
.build())
.retrieve()
.bodyToMono(Map.class);
// api 요청 - 2
Mono<Map> responseMono2 =
webClient
.get()
.uri(uriBuilder ->
uriBuilder
.path("/api/get")
.queryParam("code", code)
.build())
.retrieve()
.bodyToMono(Map.class);
// multiple api 요청
Map<String, Object> response =
Mono
.zip(responseMono1, responseMono2, (response1, response2) -> {
Map<String, Object> map = new HashMap<>();
map.put("response1", response1);
map.put("response2", response2);
return map;
})
.block();
// 결과 확인
log.info(response.toString());
}
}