From 37b787708301945c8714ff06b555dea65aa0c729 Mon Sep 17 00:00:00 2001 From: teddy Date: Fri, 30 Jan 2026 14:43:33 +0900 Subject: [PATCH] =?UTF-8?q?RestTemplateConfig=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resttemplate/ExternalHttpClient.java | 62 ++++++------------- .../resttemplate/RestTemplateConfig.java | 12 ++-- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/ExternalHttpClient.java b/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/ExternalHttpClient.java index 013e01b5..048625a4 100644 --- a/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/ExternalHttpClient.java +++ b/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/ExternalHttpClient.java @@ -7,66 +7,42 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.client.RestTemplate; +@RequiredArgsConstructor @Component @Log4j2 -@RequiredArgsConstructor public class ExternalHttpClient { private final RestTemplate restTemplate; + private final com.fasterxml.jackson.databind.ObjectMapper objectMapper; public ExternalCallResult call( String url, HttpMethod method, Object body, HttpHeaders headers, Class responseType) { - HttpEntity entity = new HttpEntity<>(body, headers); + if (headers == null) headers = new HttpHeaders(); + headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON); + headers.setAccept(java.util.List.of(org.springframework.http.MediaType.APPLICATION_JSON)); - // 요청 로그 - log.info("[HTTP-REQ] {} {}", method, url); - if (body != null) { - log.debug("[HTTP-REQ-BODY] {}", body); + try { + if (body != null) { + log.info("[HTTP-REQ-BODY-JSON] {}", objectMapper.writeValueAsString(body)); + } + } catch (Exception e) { + log.warn("[HTTP-REQ-BODY-JSON] serialize failed: {}", e.getMessage()); } + HttpEntity entity = new HttpEntity<>(body, headers); + try { ResponseEntity res = restTemplate.exchange(url, method, entity, responseType); - - int code = res.getStatusCodeValue(); - - // 응답 로그 - log.info("[HTTP-RES] {} {} -> {}", method, url, code); - log.debug("[HTTP-RES-BODY] {}", res.getBody()); - - return new ExternalCallResult<>(code, code >= 200 && code < 300, res.getBody()); - - } catch (HttpClientErrorException.NotFound e) { - log.info("[HTTP-RES] {} {} -> 404 (Not Found)", method, url); - log.debug("[HTTP-RES-BODY] {}", e.getResponseBodyAsString()); - - return new ExternalCallResult<>(404, false, null); - - } catch (HttpClientErrorException e) { - // 기타 4xx - log.warn( - "[HTTP-ERR] {} {} -> {} body={}", - method, - url, - e.getStatusCode().value(), - e.getResponseBodyAsString()); - throw e; - - } catch (HttpServerErrorException e) { - // 5xx - log.error( - "[HTTP-ERR] {} {} -> {} body={}", - method, - url, - e.getStatusCode().value(), - e.getResponseBodyAsString()); - throw e; + return new ExternalCallResult<>(res.getStatusCodeValue(), true, res.getBody(), null); + } catch (HttpStatusCodeException e) { + return new ExternalCallResult<>( + e.getStatusCode().value(), false, null, e.getResponseBodyAsString()); } } - public record ExternalCallResult(int statusCode, boolean success, T body) {} + public record ExternalCallResult(int statusCode, boolean success, T body, String errBody) {} } diff --git a/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/RestTemplateConfig.java b/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/RestTemplateConfig.java index 5b1bb394..8a8948fb 100644 --- a/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/RestTemplateConfig.java +++ b/src/main/java/com/kamco/cd/kamcoback/config/resttemplate/RestTemplateConfig.java @@ -4,6 +4,7 @@ import lombok.extern.log4j.Log4j2; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.BufferingClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @@ -13,10 +14,13 @@ public class RestTemplateConfig { @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { - SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory(); - f.setConnectTimeout(2000); - f.setReadTimeout(3000); + SimpleClientHttpRequestFactory baseFactory = new SimpleClientHttpRequestFactory(); + baseFactory.setConnectTimeout(2000); + baseFactory.setReadTimeout(3000); - return builder.requestFactory(() -> f).additionalInterceptors(new RetryInterceptor()).build(); + return builder + .requestFactory(() -> new BufferingClientHttpRequestFactory(baseFactory)) + .additionalInterceptors(new RetryInterceptor()) + .build(); } } -- 2.49.1