From e1ab548b2d9cfb9ae2b406254d932db363a7fb79 Mon Sep 17 00:00:00 2001 From: sung-silver Date: Thu, 18 Jan 2024 23:52:34 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20HttpUrlConnection=20->=20WebClient?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../external/discord/DiscordWebHook.java | 14 +++---- .../global/util/ApiCallUtil.java | 41 +++++++++---------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/external/discord/DiscordWebHook.java b/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/external/discord/DiscordWebHook.java index 16d346e..506c4cd 100644 --- a/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/external/discord/DiscordWebHook.java +++ b/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/external/discord/DiscordWebHook.java @@ -40,15 +40,11 @@ public void execute() throws IOException { throw new RuntimeException("컨텐츠를 설정하거나 하나 이상의 Embed Object를 추가해야 합니다."); } - try { - ApiCallUtil.callDiscordAppenderPostAPI( - this.urlString, createDiscordEmbedObject( - this.embeds, initializerDiscordSendForJsonObject(new JsonObject()) - )); - - } catch (IOException ioException) { - throw ioException; - } + ApiCallUtil.callDiscordAppenderPostAPI( + this.urlString, createDiscordEmbedObject( + this.embeds, initializerDiscordSendForJsonObject(new JsonObject()) + )); + } private JsonObject initializerDiscordSendForJsonObject(JsonObject json) { diff --git a/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/global/util/ApiCallUtil.java b/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/global/util/ApiCallUtil.java index a52d833..578dd56 100644 --- a/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/global/util/ApiCallUtil.java +++ b/nonsoolmateServer/src/main/java/com/nonsoolmate/nonsoolmateServer/global/util/ApiCallUtil.java @@ -1,35 +1,34 @@ package com.nonsoolmate.nonsoolmateServer.global.util; import com.nonsoolmate.nonsoolmateServer.external.discord.model.JsonObject; -import java.io.IOException; -import java.io.OutputStream; -import java.net.URL; -import javax.net.ssl.HttpsURLConnection; + +import io.jsonwebtoken.io.IOException; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; @Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE) public class ApiCallUtil { - public static void callDiscordAppenderPostAPI(String urlString, JsonObject json) throws IOException { - URL url = new URL(urlString); - HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); - connection.addRequestProperty("Content-Type", "application/json"); - connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_"); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - - try (OutputStream stream = connection.getOutputStream()) { - stream.write(json.toString().getBytes()); - stream.flush(); - - connection.getInputStream().close(); - connection.disconnect(); - - } catch (IOException ioException) { - throw ioException; + public static void callDiscordAppenderPostAPI(String urlString, JsonObject json) { + WebClient webClient = WebClient.create(); + try { + webClient.post() + .uri(urlString) + .contentType(MediaType.APPLICATION_JSON) + .header("User-Agent", "Java-DiscordWebhook-BY-Gelox_") + .body(Mono.just(json.toString()), String.class) + .retrieve() + .bodyToMono(Void.class) + .block(); + } catch (IOException e) { + log.error("Discord Appender API 호출 실패", e.getMessage()); + throw e; } } } +