Controller 의 리턴타입이 String 일 때 cannot be cast 에러 대응
This commit is contained in:
+22
-19
@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||||
import org.springframework.http.server.ServerHttpRequest;
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
import org.springframework.http.server.ServerHttpResponse;
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
@@ -25,6 +26,7 @@ public class EmptyJsonResponseAdviceController implements ResponseBodyAdvice<Obj
|
|||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(EmptyJsonResponseAdviceController.class);
|
private static final Logger logger = LoggerFactory.getLogger(EmptyJsonResponseAdviceController.class);
|
||||||
private static final Map<String, Object> EMPTY_JSON = Collections.emptyMap();
|
private static final Map<String, Object> EMPTY_JSON = Collections.emptyMap();
|
||||||
|
private static final String EMPTY_JSON_STRING = "{}";
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
@@ -42,24 +44,25 @@ public class EmptyJsonResponseAdviceController implements ResponseBodyAdvice<Obj
|
|||||||
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
|
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
|
||||||
ServerHttpResponse response) {
|
ServerHttpResponse response) {
|
||||||
|
|
||||||
// .json 요청이 아니면 원본 그대로 반환
|
String path = request.getURI().getPath();
|
||||||
String path = request.getURI().getPath();
|
|
||||||
if (path == null || !path.endsWith(".json")) {
|
// .json 요청이 아니면 원본 그대로 반환
|
||||||
return body;
|
if (path == null || !path.endsWith(".json")) {
|
||||||
}
|
return body;
|
||||||
|
}
|
||||||
// 빈 응답인 경우 빈 JSON 객체로 변환
|
|
||||||
if (body == null) {
|
// 정상적인 응답이 있는 경우, String 타입의 빈 응답 처리
|
||||||
logger.debug("빈 응답 감지 (null), {} 로 변환: {}", path);
|
if (body != null && !(body instanceof String && ((String) body).isEmpty())) {
|
||||||
return EMPTY_JSON;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
// String 타입의 빈 응답 처리
|
// Controller 리턴타입이 String 일 때 cannot be cast 에러 대응
|
||||||
if (body instanceof String && ((String) body).isEmpty()) {
|
logger.debug("빈 응답 감지, {} 로 변환 시도. 선택된 컨버터: {}", path, selectedConverterType.getSimpleName());
|
||||||
logger.debug("빈 응답 감지 (empty string), {} 로 변환: {}", path);
|
|
||||||
return EMPTY_JSON;
|
if (StringHttpMessageConverter.class.isAssignableFrom(selectedConverterType)) {
|
||||||
}
|
return EMPTY_JSON_STRING; // 문자열 "{}" 반환
|
||||||
|
}
|
||||||
return body;
|
|
||||||
|
return EMPTY_JSON; // Map 객체 반환
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user