inbound에 json 전문일 때, 최상위에 Array('[]')타입이 바로 올때, 변환처리를 위해,
KJB_ROOTLESS_ARRAY 라는 키의 Object를 warraped 필터처리.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package com.eactive.eai.adapter.controller;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
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.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ApiResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
// TODO Auto-generated method stub
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
|
||||
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
|
||||
ServerHttpResponse response) {
|
||||
|
||||
if (body instanceof String) {
|
||||
|
||||
return removeRootlessArrayKeyFormResponseBody((String) body);
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
String removeRootlessArrayKeyFormResponseBody(String body) {
|
||||
try {
|
||||
Object root = JSONValue.parse(body);
|
||||
|
||||
if (root instanceof JSONObject && ((JSONObject) root).containsKey("KJB_ROOTLESS_ARRAY")) {
|
||||
JSONArray jsonArray = (JSONArray) ((JSONObject) root).get("KJB_ROOTLESS_ARRAY");
|
||||
return jsonArray.toJSONString();
|
||||
} else {
|
||||
return body;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return body;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user