package com.vci.starter.web.autoconfigure;
|
|
import com.vci.starter.web.annotation.controller.VciUnUseResponseAdvice;
|
import com.vci.starter.web.constant.TokenKeyConstant;
|
import com.vci.starter.web.pagemodel.BaseResult;
|
import com.vci.starter.web.pagemodel.DataGrid;
|
import com.vci.starter.web.pagemodel.Tree;
|
import com.vci.starter.web.util.MessageUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.MDC;
|
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.lang.Nullable;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
|
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.List;
|
|
/**
|
* 在返回结果前,封装统一的数据对象
|
* @author weidy
|
* @date 2019/11/7 4:05 PM
|
*/
|
@ControllerAdvice(annotations = {RestController.class, Controller.class})
|
public class ResponseModifyAdvice implements ResponseBodyAdvice<Object>{
|
/**
|
* 过滤哪些需要拦截处理
|
* Whether this component supports the given controller method return type
|
* and the selected {@code HttpMessageConverter} type.
|
*
|
* @param returnType the return type
|
* @param converterType the selected converter type
|
* @return {@code true} if {@link #beforeBodyWrite} should be invoked;
|
* {@code false} otherwise
|
*/
|
@Override
|
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
//全部都支持
|
if(returnType.getMethod().isAnnotationPresent(VciUnUseResponseAdvice.class)){
|
return false;
|
}
|
return true;
|
}
|
|
/**
|
* Invoked after an {@code HttpMessageConverter} is selected and just before
|
* its write method is invoked.
|
*
|
* @param body the body to be written
|
* @param returnType the return type of the controller method
|
* @param selectedContentType the content type selected through content negotiation
|
* @param selectedConverterType the converter type selected to write to the response
|
* @param request the current request
|
* @param response the current response
|
* @return the body that was passed in or a modified (possibly new) instance
|
*/
|
@Nullable
|
@Override
|
public Object beforeBodyWrite(@Nullable Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
String traceId = MDC.get(TokenKeyConstant.TRACE_ID);
|
if(returnType.getMethod().isAnnotationPresent(VciUnUseResponseAdvice.class)){
|
return body;
|
}
|
String returnTypeName = returnType.getParameterType().getName();
|
if("void".equalsIgnoreCase(returnTypeName)){
|
BaseResult baseResult = BaseResult.success();
|
baseResult.setFinishTime(System.currentTimeMillis());
|
baseResult.setTraceId(traceId);
|
formateMsg(baseResult);
|
return baseResult;
|
}
|
if("com.vci.starter.web.pagemodel.BaseResult".equals(returnTypeName)){
|
BaseResult baseResult = ((BaseResult)body);
|
baseResult.setTraceId(traceId);
|
baseResult.setFinishTime(System.currentTimeMillis());
|
formateMsg(baseResult);
|
return baseResult;
|
}
|
if("com.vci.starter.web.pagemodel.DataGrid".equals(returnTypeName)){
|
DataGrid dataGrid = ((DataGrid)body);
|
BaseResult baseResult = BaseResult.dataGrid(dataGrid);
|
baseResult.setTraceId(traceId);
|
baseResult.setFinishTime(System.currentTimeMillis());
|
formateMsg(baseResult);
|
return baseResult;
|
}
|
if("com.vci.starter.web.pagemodel.Tree".equals(returnTypeName)){
|
//returnType.getP
|
Tree tree = ((Tree)body);
|
List<Tree> treeList = new ArrayList<>();
|
treeList.add(tree);
|
BaseResult baseResult = BaseResult.tree(treeList);
|
baseResult.setTraceId(traceId);
|
baseResult.setFinishTime(System.currentTimeMillis());
|
formateMsg(baseResult);
|
return baseResult;
|
}
|
if(body instanceof Collection){
|
//说明是集合
|
String genericParamterTypeName = returnType.getGenericParameterType().getTypeName();
|
if(genericParamterTypeName.contains("<")
|
&& genericParamterTypeName.contains(">")){
|
String elementTypeName = genericParamterTypeName.substring(genericParamterTypeName.indexOf("<")+ 1,genericParamterTypeName.indexOf(">"));
|
if("com.vci.starter.web.pagemodel.Tree".equalsIgnoreCase(elementTypeName)){
|
Collection<Tree> treeCollection = ((Collection<Tree>)body);
|
BaseResult baseResult = BaseResult.tree(treeCollection);
|
baseResult.setTraceId(traceId);
|
baseResult.setFinishTime(System.currentTimeMillis());
|
formateMsg(baseResult);
|
return baseResult;
|
}else{
|
//放到Obj属性里
|
Collection<Object> objectCollection = ((Collection<Object>)body);
|
BaseResult baseResult = BaseResult.success(objectCollection);
|
baseResult.setTraceId(traceId);
|
baseResult.setFinishTime(System.currentTimeMillis());
|
formateMsg(baseResult);
|
return baseResult;
|
}
|
}
|
}
|
if(body instanceof Object){
|
BaseResult baseResult = BaseResult.success((Object)body);
|
baseResult.setTraceId(traceId);
|
baseResult.setFinishTime(System.currentTimeMillis());
|
formateMsg(baseResult);
|
return baseResult;
|
}
|
return body;
|
}
|
|
/**
|
* 多语格式化
|
* @param baseResult 结果对象
|
*/
|
private void formateMsg(BaseResult baseResult ){
|
if(baseResult!=null && StringUtils.isNotBlank(baseResult.getMsg())){
|
baseResult.setMsg(MessageUtils.get(baseResult.getMsg(),baseResult.getMsgObjs()));
|
}
|
}
|
}
|