.json 요청에서 빈 응답 반환 시 {} 로 변환하는 ResponseBodyAdvice 추가
This commit is contained in:
@@ -78,6 +78,9 @@
|
||||
<context:exclude-filter type="aspectj"
|
||||
expression="!com.eactive.eai.rms..*Controller" />
|
||||
</context:component-scan>
|
||||
|
||||
<!-- .json 요청에서 빈 응답을 {} 로 변환하는 ResponseBodyAdvice -->
|
||||
<bean class="com.eactive.eai.rms.common.advice.EmptyJsonResponseAdviceController" />
|
||||
<mvc:interceptors>
|
||||
<mvc:interceptor>
|
||||
<mvc:mapping path="/**" />
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.eactive.eai.rms.common.advice;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* .json 요청에 대해 빈 응답 본문을 {}로 변환하는 ResponseBodyAdvice.
|
||||
*
|
||||
* 웹서버를 거치면서 Content-Type이 application/json으로 설정되는데,
|
||||
* 빈 응답이면 JavaScript(jQuery)가 JSON 파싱 시 에러가 발생하는 문제를 해결합니다.
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class EmptyJsonResponseAdviceController implements ResponseBodyAdvice<Object> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(EmptyJsonResponseAdviceController.class);
|
||||
private static final Map<String, Object> EMPTY_JSON = Collections.emptyMap();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
logger.info("========== EmptyJsonResponseAdviceController 로딩됨 ==========");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
// 모든 응답에 대해 적용 (beforeBodyWrite에서 .json 요청만 필터링)
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
|
||||
Class<? extends HttpMessageConverter<?>> 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.eactive.eai.rms.common.advice;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
/**
|
||||
* EmptyJsonResponseAdviceController 테스트용 컨트롤러.
|
||||
* 테스트 후 삭제 필요.
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/test/empty-json")
|
||||
public class EmptyJsonResponseTestController {
|
||||
|
||||
/**
|
||||
* null 반환 테스트
|
||||
* URL: /monitoring/test/empty-json/null.json
|
||||
*/
|
||||
@RequestMapping("/null.json")
|
||||
@ResponseBody
|
||||
public Object returnNull() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 빈 문자열 반환 테스트
|
||||
* URL: /monitoring/test/empty-json/empty-string.json
|
||||
*/
|
||||
@RequestMapping("/empty-string.json")
|
||||
@ResponseBody
|
||||
public String returnEmptyString() {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 정상 데이터 반환 테스트 (변환되지 않아야 함)
|
||||
* URL: /monitoring/test/empty-json/normal.json
|
||||
*/
|
||||
@RequestMapping("/normal.json")
|
||||
@ResponseBody
|
||||
public String returnNormal() {
|
||||
return "{\"status\":\"ok\"}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user