diff --git a/src/main/java/com/eactive/eai/rms/common/advice/EmptyJsonResponseAdviceController.java b/src/main/java/com/eactive/eai/rms/common/advice/EmptyJsonResponseAdviceController.java index 525f5be..c98bed7 100644 --- a/src/main/java/com/eactive/eai/rms/common/advice/EmptyJsonResponseAdviceController.java +++ b/src/main/java/com/eactive/eai/rms/common/advice/EmptyJsonResponseAdviceController.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -25,6 +26,7 @@ public class EmptyJsonResponseAdviceController implements ResponseBodyAdvice EMPTY_JSON = Collections.emptyMap(); + private static final String EMPTY_JSON_STRING = "{}"; @PostConstruct public void init() { @@ -42,24 +44,25 @@ public class EmptyJsonResponseAdviceController implements ResponseBodyAdvice> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { - // .json 요청이 아니면 원본 그대로 반환 - String path = request.getURI().getPath(); - if (path == null || !path.endsWith(".json")) { - return body; - } - - // 빈 응답인 경우 빈 JSON 객체로 변환 - if (body == null) { - logger.debug("빈 응답 감지 (null), {} 로 변환: {}", path); - return EMPTY_JSON; - } - - // String 타입의 빈 응답 처리 - if (body instanceof String && ((String) body).isEmpty()) { - logger.debug("빈 응답 감지 (empty string), {} 로 변환: {}", path); - return EMPTY_JSON; - } - - return body; + String path = request.getURI().getPath(); + + // .json 요청이 아니면 원본 그대로 반환 + if (path == null || !path.endsWith(".json")) { + return body; + } + + // 정상적인 응답이 있는 경우, String 타입의 빈 응답 처리 + if (body != null && !(body instanceof String && ((String) body).isEmpty())) { + return body; + } + + // Controller 리턴타입이 String 일 때 cannot be cast 에러 대응 + logger.debug("빈 응답 감지, {} 로 변환 시도. 선택된 컨버터: {}", path, selectedConverterType.getSimpleName()); + + if (StringHttpMessageConverter.class.isAssignableFrom(selectedConverterType)) { + return EMPTY_JSON_STRING; // 문자열 "{}" 반환 + } + + return EMPTY_JSON; // Map 객체 반환 } }