|
|
@@ -1,140 +1,140 @@
|
|
|
-package com.storlead.framework.web.process;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.core.JsonParser;
|
|
|
-import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.storlead.framework.web.message.WebResp;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-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.*;
|
|
|
-import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
-
|
|
|
-import java.lang.reflect.AnnotatedElement;
|
|
|
-import java.lang.reflect.Array;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-@ControllerAdvice
|
|
|
-public class ApiResultHandler implements ResponseBodyAdvice<Object> {
|
|
|
- private List<String> modulePackages;
|
|
|
-
|
|
|
- private static final Class[] annos = {
|
|
|
- RequestMapping.class
|
|
|
- , GetMapping.class
|
|
|
- , PostMapping.class
|
|
|
- , DeleteMapping.class
|
|
|
- , PutMapping.class
|
|
|
- };
|
|
|
-
|
|
|
+//package com.storlead.framework.web.process;
|
|
|
+//
|
|
|
+//import com.fasterxml.jackson.core.JsonParser;
|
|
|
+//import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+//import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+//import com.storlead.framework.web.message.WebResp;
|
|
|
+//import org.apache.commons.lang3.StringUtils;
|
|
|
+//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.*;
|
|
|
+//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
+//
|
|
|
+//import java.lang.reflect.AnnotatedElement;
|
|
|
+//import java.lang.reflect.Array;
|
|
|
+//import java.util.*;
|
|
|
+//
|
|
|
+//@ControllerAdvice
|
|
|
+//public class ApiResultHandler implements ResponseBodyAdvice<Object> {
|
|
|
+// private List<String> modulePackages;
|
|
|
+//
|
|
|
+// private static final Class[] annos = {
|
|
|
+// RequestMapping.class
|
|
|
+// , GetMapping.class
|
|
|
+// , PostMapping.class
|
|
|
+// , DeleteMapping.class
|
|
|
+// , PutMapping.class
|
|
|
+// };
|
|
|
+//
|
|
|
+//// @Override
|
|
|
+//// public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
+//// return supports(returnType, modulePackages);
|
|
|
+//// }
|
|
|
+//
|
|
|
// @Override
|
|
|
-// public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
|
|
-// return supports(returnType, modulePackages);
|
|
|
+// public boolean supports(MethodParameter returnType, Class converterType) {
|
|
|
+// AnnotatedElement element = returnType.getAnnotatedElement();
|
|
|
+// return Arrays.stream(annos).anyMatch(anno -> anno.isAnnotation() && element.isAnnotationPresent(anno));
|
|
|
// }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean supports(MethodParameter returnType, Class converterType) {
|
|
|
- AnnotatedElement element = returnType.getAnnotatedElement();
|
|
|
- return Arrays.stream(annos).anyMatch(anno -> anno.isAnnotation() && element.isAnnotationPresent(anno));
|
|
|
- }
|
|
|
-
|
|
|
-// public static boolean supports(MethodParameter returnType, List<String> modulePackages) {
|
|
|
-// Class<?> declaringClass = returnType.getDeclaringClass();
|
|
|
-// return belongOwn(modulePackages, declaringClass.getPackage().getName());
|
|
|
-//// declaringClass.getAnnotation(UnWarp.class) == null
|
|
|
-//// && returnType.getMethodAnnotation(Unwrap.class) == null
|
|
|
-//// && belongOwn(modulePackages, declaringClass.getPackage().getName());
|
|
|
+//
|
|
|
+//// public static boolean supports(MethodParameter returnType, List<String> modulePackages) {
|
|
|
+//// Class<?> declaringClass = returnType.getDeclaringClass();
|
|
|
+//// return belongOwn(modulePackages, declaringClass.getPackage().getName());
|
|
|
+////// declaringClass.getAnnotation(UnWarp.class) == null
|
|
|
+////// && returnType.getMethodAnnotation(Unwrap.class) == null
|
|
|
+////// && belongOwn(modulePackages, declaringClass.getPackage().getName());
|
|
|
+//// }
|
|
|
+//
|
|
|
+// public static boolean belongOwn(List<String> modulePackages, String declaringPackageName) {
|
|
|
+// for (String modulePackage : modulePackages) {
|
|
|
+// if (declaringPackageName.startsWith(modulePackage)) {
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return false;
|
|
|
// }
|
|
|
-
|
|
|
- public static boolean belongOwn(List<String> modulePackages, String declaringPackageName) {
|
|
|
- for (String modulePackage : modulePackages) {
|
|
|
- if (declaringPackageName.startsWith(modulePackage)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 数据返回之前 处理数据
|
|
|
- **/
|
|
|
- @Override
|
|
|
- public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
|
|
|
- Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
|
|
|
- ServerHttpResponse response) {
|
|
|
-
|
|
|
- if (MediaType.TEXT_EVENT_STREAM.includes(selectedContentType)) {
|
|
|
- return body;
|
|
|
- }
|
|
|
-
|
|
|
- Object data = wrapIfNeed(body, returnType);
|
|
|
- String selectedConverterTypeClassName = selectedConverterType.getName();
|
|
|
- // selectedConverterType 对象是 StringHttpMessageConverter时, 表示不能直接返回 WebResp, 要不然会报错 org.springframework.http.converter.StringHttpMessageConverter
|
|
|
- if (selectedConverterTypeClassName.indexOf("StringHttpMessageConverter") >= 0) { // 如果是返回 string 类型的, 就返回 string 类型的
|
|
|
- if (data instanceof WebResp) {
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
- mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
|
- mapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
|
|
|
- mapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
|
|
|
- mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
|
|
|
- try {
|
|
|
- return mapper.writeValueAsString(data);
|
|
|
- } catch (Exception e) {
|
|
|
- return data;
|
|
|
- }
|
|
|
- } else {
|
|
|
- return data;
|
|
|
- }
|
|
|
- } else { // 返回 对象的
|
|
|
- return data;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static Object wrapIfNeed(Object body, MethodParameter returnType) {
|
|
|
- if (body instanceof WebResp) {
|
|
|
- return body;
|
|
|
- } else {
|
|
|
- if (body != null) {
|
|
|
- return new WebResp(body);
|
|
|
- } else if(body == null) {
|
|
|
- return new WebResp("");
|
|
|
- }
|
|
|
- return new WebResp(getDefaultValue(returnType));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static Object getDefaultValue(MethodParameter returnType) {
|
|
|
- Class<?> bodyType = returnType.getParameterType();
|
|
|
- return getDefaultValue(bodyType);
|
|
|
- }
|
|
|
-
|
|
|
- private static Object getDefaultValue(Class<?> bodyType) {
|
|
|
- final Object body;
|
|
|
- if (bodyType.isArray()) {
|
|
|
- body = Array.newInstance(bodyType.getComponentType(), 0);
|
|
|
- } else if (List.class.isAssignableFrom(bodyType)) {
|
|
|
- body = Collections.emptyList();
|
|
|
- } else if (Set.class.isAssignableFrom(bodyType)) {
|
|
|
- body = Collections.emptySet();
|
|
|
- } else if (Map.class.isAssignableFrom(bodyType)) {
|
|
|
- body = Collections.emptyMap();
|
|
|
- } else if (String.class.isAssignableFrom(bodyType)) {
|
|
|
- body = StringUtils.EMPTY;
|
|
|
- } else {
|
|
|
- body = bodyType;
|
|
|
- }
|
|
|
- return body;
|
|
|
- }
|
|
|
-
|
|
|
-//BeansException
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 数据返回之前 处理数据
|
|
|
+// **/
|
|
|
// @Override
|
|
|
-// public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
|
-//// List<String> modulePackages = FrameworkAutoConfigurationPackages.get(beanFactory);
|
|
|
-//// List<String> modulePackageList = new ArrayList<>(modulePackages.size() + 1);
|
|
|
-//// modulePackageList.addAll(modulePackages);
|
|
|
-//// modulePackageList.add(PageResp.class.getPackage().getName());
|
|
|
-// this.modulePackages = null;//= modulePackageList;
|
|
|
+// public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
|
|
|
+// Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
|
|
|
+// ServerHttpResponse response) {
|
|
|
+//
|
|
|
+// if (MediaType.TEXT_EVENT_STREAM.includes(selectedContentType)) {
|
|
|
+// return body;
|
|
|
+// }
|
|
|
+//
|
|
|
+// Object data = wrapIfNeed(body, returnType);
|
|
|
+// String selectedConverterTypeClassName = selectedConverterType.getName();
|
|
|
+// // selectedConverterType 对象是 StringHttpMessageConverter时, 表示不能直接返回 WebResp, 要不然会报错 org.springframework.http.converter.StringHttpMessageConverter
|
|
|
+// if (selectedConverterTypeClassName.indexOf("StringHttpMessageConverter") >= 0) { // 如果是返回 string 类型的, 就返回 string 类型的
|
|
|
+// if (data instanceof WebResp) {
|
|
|
+// ObjectMapper mapper = new ObjectMapper();
|
|
|
+// mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
|
+// mapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
|
|
|
+// mapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
|
|
|
+// mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
|
|
|
+// try {
|
|
|
+// return mapper.writeValueAsString(data);
|
|
|
+// } catch (Exception e) {
|
|
|
+// return data;
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// return data;
|
|
|
+// }
|
|
|
+// } else { // 返回 对象的
|
|
|
+// return data;
|
|
|
+// }
|
|
|
// }
|
|
|
-}
|
|
|
+//
|
|
|
+// public static Object wrapIfNeed(Object body, MethodParameter returnType) {
|
|
|
+// if (body instanceof WebResp) {
|
|
|
+// return body;
|
|
|
+// } else {
|
|
|
+// if (body != null) {
|
|
|
+// return new WebResp(body);
|
|
|
+// } else if(body == null) {
|
|
|
+// return new WebResp("");
|
|
|
+// }
|
|
|
+// return new WebResp(getDefaultValue(returnType));
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static Object getDefaultValue(MethodParameter returnType) {
|
|
|
+// Class<?> bodyType = returnType.getParameterType();
|
|
|
+// return getDefaultValue(bodyType);
|
|
|
+// }
|
|
|
+//
|
|
|
+// private static Object getDefaultValue(Class<?> bodyType) {
|
|
|
+// final Object body;
|
|
|
+// if (bodyType.isArray()) {
|
|
|
+// body = Array.newInstance(bodyType.getComponentType(), 0);
|
|
|
+// } else if (List.class.isAssignableFrom(bodyType)) {
|
|
|
+// body = Collections.emptyList();
|
|
|
+// } else if (Set.class.isAssignableFrom(bodyType)) {
|
|
|
+// body = Collections.emptySet();
|
|
|
+// } else if (Map.class.isAssignableFrom(bodyType)) {
|
|
|
+// body = Collections.emptyMap();
|
|
|
+// } else if (String.class.isAssignableFrom(bodyType)) {
|
|
|
+// body = StringUtils.EMPTY;
|
|
|
+// } else {
|
|
|
+// body = bodyType;
|
|
|
+// }
|
|
|
+// return body;
|
|
|
+// }
|
|
|
+//
|
|
|
+////BeansException
|
|
|
+//// @Override
|
|
|
+//// public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
|
+////// List<String> modulePackages = FrameworkAutoConfigurationPackages.get(beanFactory);
|
|
|
+////// List<String> modulePackageList = new ArrayList<>(modulePackages.size() + 1);
|
|
|
+////// modulePackageList.addAll(modulePackages);
|
|
|
+////// modulePackageList.add(PageResp.class.getPackage().getName());
|
|
|
+//// this.modulePackages = null;//= modulePackageList;
|
|
|
+//// }
|
|
|
+//}
|