๐ BodyPublisher, BodyHandler ์ญํ
๐ HttpClient ์ค์ต
BodyPublisher? BodyHandler?
BodyPublisher์ BodyHandlers๋ ์์ฒญ๊ณผ ์๋ต์ ๋ณธ๋ฌธ์ ์ฒ๋ฆฌํ๋ ๋ฐ ์์ด ์ํธ ๋ณด์์ ์ธ ์ญํ ์ ํ๋ค. BodyPublisher๋ ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ๋ก ์ ์กํ ๋ฐ์ดํฐ๋ฅผ ์ ์ํ๊ณ , BodyHandlers๋ ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง๋ฅผ ์ ์ํ๋ค. ์ด ๋ ๊ตฌ์ฑ ์์๋ฅผ ์ ์ ํ ํ์ฉํ๋ฉด, HTTP ํต์ ์ ๋์ฑ ์ ์ฐํ๊ณ ํจ์จ์ ์ผ๋ก ๊ตฌํํ ์ ์๋ค.
BodyPublisher
- HTTP ์์ฒญ์ ๋ณธ๋ฌธ์ ์ ๊ณต
- ๋ฐ์ดํฐ๋ฅผ ์ ์กํ ๋ ์ด๋ป๊ฒ ์ ๊ณตํ ์ง ์ ์
BodyPublishers.ofString(String): ๋ฌธ์์ด ๋ฐ์ดํฐBodyPublishers.ofByteArray(byte[]): ๋ฐ์ดํธ ๋ฐฐ์ด ๋ฐ์ดํฐBodyPublishers.ofFile(Path): ํ์ผ ๋ฐ์ดํฐBodyPublishers.ofInputStream(Supplier<InputStream>): ๋์ ์ผ๋ก ์์ฑ๋๋ ์ ๋ ฅ ์คํธ๋ฆผ
BodyHandler
- HTTP ์๋ต์ ๋ณธ๋ฌธ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์ ์
- ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง ์ ์
BodyHandlers.ofString(): ๋ฌธ์์ด ๋ฐ์ดํฐBodyHandlers.ofByteArray(): ๋ฐ์ดํธ ๋ฐฐ์ด ๋ฐ์ดํฐBodyHandlers.ofFile(Path): ํ์ผ ๋ฐ์ดํฐBodyHandlers.ofInputStream():InputStream์ผ๋ก ๋ฐ์ดํฐ ์ ๊ณต
HttpResponse.BodyHandler ํด๋์ค๋ BodyHandler๋ฅผ ์์ฑํ๊ธฐ ์ํ ์ฌ๋ฌ ๊ฐ์ง ํธ๋ฆฌํ ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค. ์ด๋ค ์ค ๋ค์๋ ์๋ต ๋ฐ์ดํธ๊ฐ ์์ ํ ์์ ๋ ๋๊น์ง ๋ฉ๋ชจ๋ฆฌ์ ์ถ์ ๋๋ฉฐ, ๊ทธ ํ ์๋ต ๋ฐ์ดํธ๋ ์์ ์์ค์ Java ์ ํ(ofString, ofByteArray ๋ฑ)์ผ๋ก ๋ณํ๋๋ค.
Get
๋๊ธฐ
BodyHandler๋ ์๋ต ์ํ ์ฝ๋์ ํค๋๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํด์ง๋ฉด ์๋ต ๋ณธ๋ฌธ ๋ฐ์ดํธ๊ฐ ์์ ๋๊ธฐ ์ ์ ํธ์ถ
public void get(String uri, String param) throws Exception {
String uriWithParam = uri+"?"+param;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uriWithParam))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Sync status code: "+ response.statusCode());
}
๋น๋๊ธฐ
- ๋น๋๊ธฐ API๋ ์ฌ์ฉ ๊ฐ๋ฅํด์ง๋ฉด
HttpResponse๋ก ์๋ฃ๋๋CompletableFuture๋ฅผ ์ฆ์ ๋ฐํ CompletableFuture.thenApply(Function)๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌHttpResponse๋ฅผ ๋ณธ๋ฌธ ์ ํ, ์ํ ์ฝ๋ ๋ฑ์ ๋งคํํ ์ ์๋ค.join(): ๋น๋๊ธฐ ์์ ์ด ๋๋ ๋๊น์ง ๋๊ธฐ
public void getAsync(String uri, String param) {
String uriWithParam = uri+"?"+param;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uriWithParam))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::statusCode)
.thenAccept(statusCode -> System.out.println("Async status code: " + statusCode))
.join();
}
Post
discard BodyHandler๋ ๊ด์ฌ์ด ์๋ ์๋ต ๋ณธ๋ฌธ์ ์์ ํ๊ณ ์ญ์ ํ๋ ๋ฐ ์ฌ์ฉ๋ ์ ์๋ค.
public void post(String uri, String data) throws Exception {
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.POST(HttpRequest.BodyPublishers.ofString(data))
.build();
HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.discarding());
System.out.println("Post status code: " + response.statusCode());
}
์ค์ต
์์
๋ค์ด๋ฒ ๋ก๊ทธ์ธ ํธ์ถ์ ๋ํ ์์ ์ด๋ค. ๊ฐ์ธ ํ๋ก์ ํธ์ ์ ์ฉ ์ค์ธ ์ค์ ์ผ๋ก requestUrl๊ณผ redirectUrl์ ์ด๋ฏธ ์ค์ ๋์ด์๋ ๊ฐ์ผ๋ก ์ฌ์ฉํ๋ค.
์ค์ ๊ฒฐ๊ณผ๋ ํ์ด์ง๊ฐ ๋ฆฌ๋ค์ด๋ ํธํ๋ฉด์ text/html ํ์
์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ์ํ์ฝ๋๋ง ์ถ๋ ฅํ๋๋ก ํ๋ค.
public static void main(String[] args) {
HttpClientExample httpClientExample = new HttpClientExample();
String requestUrl = "https://nid.naver.com/oauth2.0/authorize";
String redirectUrl = "https://4d4cat.site/login/naver/callback";
String clientKey = "๋ค์ด๋ฒํค๊ฐ";
NaverLoginRequest requestParam = new NaverLoginRequest("code", clientKey, redirectUrl, "state");
try {
httpClientExample.get(requestUrl, requestParam.toString());
httpClientExample.getAsync(requestUrl, requestParam.toString());
httpClientExample.post(requestUrl, requestParam.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
์ถ๋ ฅ
Sync status code: 200
Async status code: 200
Post status code: 200
๊ณต๋ถํด์ผํ๋ ๊ฐ๋
- BodySubscriber
- CompletableFuture
- Backpressure