dangsn
2024-06-06 33321f5486fd586fda6fd3f46b7e71754fede28b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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()));
        }
    }
}