RestTemplateConfig 수정
This commit is contained in:
@@ -22,7 +22,10 @@ public class ExternalHttpClient {
|
||||
private final RestTemplate restTemplate;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
/** responseType(예: DTO.class, String.class 등)로 그대로 역직렬화해서 받는 호출. - 응답 타입이 명확히 고정일 때 사용 */
|
||||
/**
|
||||
* - responseType이 DTO면: DTO로 역직렬화 - responseType이 String.class면: 응답을 byte[]로 받아 UTF-8 원문 문자열로 반환
|
||||
* (배열/객체 등 유동 JSON 안전)
|
||||
*/
|
||||
public <T> ExternalCallResult<T> call(
|
||||
String url, HttpMethod method, Object body, HttpHeaders headers, Class<T> responseType) {
|
||||
|
||||
@@ -32,31 +35,19 @@ public class ExternalHttpClient {
|
||||
HttpEntity<Object> entity = new HttpEntity<>(body, resolvedHeaders);
|
||||
|
||||
try {
|
||||
if (responseType == String.class) {
|
||||
ResponseEntity<byte[]> res = restTemplate.exchange(url, method, entity, byte[].class);
|
||||
String raw =
|
||||
(res.getBody() == null) ? null : new String(res.getBody(), StandardCharsets.UTF_8);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
T casted = (T) raw; // responseType == String.class 이므로 안전
|
||||
return new ExternalCallResult<>(res.getStatusCodeValue(), true, casted, null);
|
||||
}
|
||||
|
||||
ResponseEntity<T> res = restTemplate.exchange(url, method, entity, responseType);
|
||||
return new ExternalCallResult<>(res.getStatusCodeValue(), true, res.getBody(), null);
|
||||
} catch (HttpStatusCodeException e) {
|
||||
return new ExternalCallResult<>(
|
||||
e.getStatusCode().value(), false, null, e.getResponseBodyAsString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 응답이 배열/객체/문자열/숫자 등 무엇이든 상관없이 "원문(JSON 문자열)"로 안전하게 받는 호출. - 응답 구조가 유동적이거나, String.class로 받았다가
|
||||
* Jackson 컨버터 때문에 깨지는 케이스에 사용
|
||||
*/
|
||||
public ExternalCallResult<String> callRaw(
|
||||
String url, HttpMethod method, Object body, HttpHeaders headers) {
|
||||
|
||||
HttpHeaders resolvedHeaders = resolveJsonHeaders(headers);
|
||||
logRequestBody(body);
|
||||
|
||||
HttpEntity<Object> entity = new HttpEntity<>(body, resolvedHeaders);
|
||||
|
||||
try {
|
||||
ResponseEntity<byte[]> res = restTemplate.exchange(url, method, entity, byte[].class);
|
||||
String raw =
|
||||
(res.getBody() == null) ? null : new String(res.getBody(), StandardCharsets.UTF_8);
|
||||
return new ExternalCallResult<>(res.getStatusCodeValue(), true, raw, null);
|
||||
} catch (HttpStatusCodeException e) {
|
||||
return new ExternalCallResult<>(
|
||||
e.getStatusCode().value(), false, null, e.getResponseBodyAsString());
|
||||
@@ -66,7 +57,6 @@ public class ExternalHttpClient {
|
||||
private HttpHeaders resolveJsonHeaders(HttpHeaders headers) {
|
||||
HttpHeaders h = (headers == null) ? new HttpHeaders() : headers;
|
||||
|
||||
// 이미 세팅돼 있으면 존중하고, 없으면 JSON 기본값 세팅
|
||||
if (h.getContentType() == null) {
|
||||
h.setContentType(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user