springboot返回结果统一处理

一般来说异常统一处理都知道,@RestControllerAdvice和@ControllerAdive,然后使用@ExceptionHandler注解处理异常统一处理即可。如今前后端分离情况居多,返回给前端的我们也需要统一包装一下,比方说:

package com.lhf.fvscommon.result;

import lombok.Data;

import java.io.Serializable;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/10/22 10:26
 */

@Data
public class Result<T> {

    private int code;

    private T t;

    private String mes;

    public Result(int code, T t, String mes) {
        this.code = code;
        this.t = t;
        this.mes = mes;
    }


    public static <T> Result<T> success() {
        Status success = Status.SUCCESS;
        return new Result<>(success.code, null, success.mes);
    }

    /**
     * 请求成功默认返回
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T t) {
        Status success = Status.SUCCESS;
        return new Result<>(success.code, t, success.mes);
    }

    /**
     * 请求成功自定义状态返回
     *
     * @param t
     * @param status
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T t, Status status) {
        return new Result<>(status.code, t, status.mes);
    }

    /**
     * 请求失败默认返回
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure() {
        Status failure = Status.FAILURE;
        return new Result<>(failure.code, null, failure.mes);
    }

    /**
     * 自定义失败返回状态,返回信息
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(Status status, String mes) {
        return new Result<>(status.code, null, mes);
    }

    /**
     * 请求失败自定义状态
     *
     * @param status
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure(Status status) {
        return new Result<>(status.code, null, status.mes);
    }


    /**
     * 构建
     *
     * @param <T>
     * @return
     */
    public static <T> Builder<T> builder() {
        return new Builder<>();
    }

    public static class Builder<T> {
        private int code;
        private T t;
        private String mes;

        public Builder<T> code(int code) {
            this.code = code;
            return this;
        }

        public Builder<T> t(T t) {
            this.t = t;
            return this;
        }

        public Builder<T> mes(String mes) {
            this.mes = mes;
            return this;
        }

        public Result<T> build() {
            return new Result<>(this.code, this.t, this.mes);
        }
    }
}

上诉代码就是一个基本的返回统一处理的类,相比大家都不陌生,不过恶心的就是每次返回都需要手动包装啊,这个就有点恶心了啊!能不能简单点?

期间我想过aop,但是aop环绕增强返回的一定是实际返回的一个子类,于是要么我们使用一个公共类,Result继承这个公共类,并且所有的返回前端数据对象都继承他,好像也不是很好。而且还有一个潜在的问题就是万一返回的是个String拿不完犊子?要么,实际返回就是一个Object,那这样的话代码可读性就比较差了(别人读你的代码的时候并不知道你返回的是什么啊!!!),于是有改变策略使用了一下过滤器,但是我发现也不是很好(不太好处理)。最后我吧目标放到Springboot自带的Json处理上,因为他的处理和我们要的结果好像是类似!

然后我就debug,跟了好几次终于确定了位置:看下边的代码(代码有点长哈)

    protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
            ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
            throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

        Object body;
        Class<?> valueType;
        Type targetType;

        if (value instanceof CharSequence) {
            body = value.toString();
            valueType = String.class;
            targetType = String.class;
        }
        else {
            body = value;
            valueType = getReturnValueType(body, returnType);
            targetType = GenericTypeResolver.resolveType(getGenericType(returnType), returnType.getContainingClass());
        }

        if (isResourceType(value, returnType)) {
            outputMessage.getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
            if (value != null && inputMessage.getHeaders().getFirst(HttpHeaders.RANGE) != null &&
                    outputMessage.getServletResponse().getStatus() == 200) {
                Resource resource = (Resource) value;
                try {
                    List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
                    outputMessage.getServletResponse().setStatus(HttpStatus.PARTIAL_CONTENT.value());
                    body = HttpRange.toResourceRegions(httpRanges, resource);
                    valueType = body.getClass();
                    targetType = RESOURCE_REGION_LIST_TYPE;
                }
                catch (IllegalArgumentException ex) {
                    outputMessage.getHeaders().set(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
                    outputMessage.getServletResponse().setStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value());
                }
            }
        }

        MediaType selectedMediaType = null;
        MediaType contentType = outputMessage.getHeaders().getContentType();
        boolean isContentTypePreset = contentType != null && contentType.isConcrete();
        if (isContentTypePreset) {
            if (logger.isDebugEnabled()) {
                logger.debug("Found 'Content-Type:" + contentType + "' in response");
            }
            selectedMediaType = contentType;
        }
        else {
            HttpServletRequest request = inputMessage.getServletRequest();
            List<MediaType> acceptableTypes = getAcceptableMediaTypes(request);
            List<MediaType> producibleTypes = getProducibleMediaTypes(request, valueType, targetType);

            if (body != null && producibleTypes.isEmpty()) {
                throw new HttpMessageNotWritableException(
                        "No converter found for return value of type: " + valueType);
            }
            List<MediaType> mediaTypesToUse = new ArrayList<>();
            for (MediaType requestedType : acceptableTypes) {
                for (MediaType producibleType : producibleTypes) {
                    if (requestedType.isCompatibleWith(producibleType)) {
                        mediaTypesToUse.add(getMostSpecificMediaType(requestedType, producibleType));
                    }
                }
            }
            if (mediaTypesToUse.isEmpty()) {
                if (body != null) {
                    throw new HttpMediaTypeNotAcceptableException(producibleTypes);
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("No match for " + acceptableTypes + ", supported: " + producibleTypes);
                }
                return;
            }

            MediaType.sortBySpecificityAndQuality(mediaTypesToUse);

            for (MediaType mediaType : mediaTypesToUse) {
                if (mediaType.isConcrete()) {
                    selectedMediaType = mediaType;
                    break;
                }
                else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
                    selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
                    break;
                }
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Using '" + selectedMediaType + "', given " +
                        acceptableTypes + " and supported " + producibleTypes);
            }
        }

        if (selectedMediaType != null) {
            selectedMediaType = selectedMediaType.removeQualityValue();
            for (HttpMessageConverter<?> converter : this.messageConverters) {
                GenericHttpMessageConverter genericConverter = (converter instanceof GenericHttpMessageConverter ?
                        (GenericHttpMessageConverter<?>) converter : null);
                if (genericConverter != null ?
                        ((GenericHttpMessageConverter) converter).canWrite(targetType, valueType, selectedMediaType) :
                        converter.canWrite(valueType, selectedMediaType)) {
                    body = getAdvice().beforeBodyWrite(body, returnType, selectedMediaType,
                            (Class<? extends HttpMessageConverter<?>>) converter.getClass(),
                            inputMessage, outputMessage);
                    if (body != null) {
                        Object theBody = body;
                        LogFormatUtils.traceDebug(logger, traceOn ->
                                "Writing [" + LogFormatUtils.formatValue(theBody, !traceOn) + "]");
                        addContentDispositionHeader(inputMessage, outputMessage);
                        if (genericConverter != null) {
                            genericConverter.write(body, targetType, selectedMediaType, outputMessage);
                        }
                        else {
                            ((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);
                        }
                    }
                    else {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Nothing to write: null body");
                        }
                    }
                    return;
                }
            }
        }

        if (body != null) {
            Set<MediaType> producibleMediaTypes =
                    (Set<MediaType>) inputMessage.getServletRequest()
                            .getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);

            if (isContentTypePreset || !CollectionUtils.isEmpty(producibleMediaTypes)) {
                throw new HttpMessageNotWritableException(
                        "No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
            }
            throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
        }
    }

重点我们看第二个红色的部分:getAdvice()返回的是RequestResponseBodyAdviceChain,呦吼看着名字应该是责任链模式了。当然这不是重点,偶尔皮一下,就不那么无聊了。那么重点是这个类了,

 

 从图片上不难看出,这个玩意儿就是处理返回的呀。我们再来看看ResponseBodyAdvice,毕竟我们重点关注的也应该是这个!!!

 

 他就两个方法supports和beforeBodyWrite,supports:此组件是否支持给定的控制器方法返回类型和所选的{@code HttpMessageConverter}类型,但是spring源码并没有给出这个方法的实现而是直接抛出一个异常(当然一定是有处理的,往后看啦)

 

 就很憨憨呀,来看下一个beforeBodyWrite:从方法名字不难发现,响应体写入之前的操作。

 

 

 可以看到这个返回处理的类是数组,也就是说我能不能继承一个,重写一个beforeBodyWrite呢?

 

 

 以上变这段代码看来,可以看到一个ControllerAdviceBean,从名字来看,不难发现应该是一个@ControllerAdvice标记的Bean对象没错了。availableAdvice变量是什么,追踪源码发现就是ResponseBodyAdvice集合,最终我得出的结论就是我们自定义的ResponseBodyAdvice类将是一个@ControllerAdvice标记的类。现在来试一下吧!!!

 1 package com.lhf.fvscore.result.advice;
 2 
 3 import com.fasterxml.jackson.core.JsonProcessingException;
 4 import com.fasterxml.jackson.databind.ObjectMapper;
 5 import com.lhf.fvscommon.result.Result;
 6 import com.lhf.fvscore.annotation.ResultBody;
 7 import org.springframework.core.MethodParameter;
 8 import org.springframework.http.MediaType;
 9 import org.springframework.http.server.ServerHttpRequest;
10 import org.springframework.http.server.ServerHttpResponse;
11 import org.springframework.web.bind.annotation.*;
12 import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
13 
14 import java.lang.reflect.AnnotatedElement;
15 import java.util.Arrays;
16 
17 /**
18  * <p>
19  *
20  * </p>
21  *
22  * @author lhf
23  * @since 2020/11/6 14:30
24  */
25 @ControllerAdvice(annotations = {ResultBody.class})
26 public class ResultAdvise implements ResponseBodyAdvice<Object> {
27 
28     private final ThreadLocal<ObjectMapper> threadLocal = ThreadLocal.withInitial(ObjectMapper::new);
29 
30     private static final Class[] annos = {
31             RequestMapping.class,
32             GetMapping.class,
33             PostMapping.class,
34             DeleteMapping.class,
35             PutMapping.class,
36             PatchMapping.class
37     };
38 
39     @Override
40     public boolean supports(MethodParameter methodParameter, Class aClass) {
41         return this.validateMethod(methodParameter);
42     }
43 
44     @Override
45     public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest request, ServerHttpResponse response) {
46 
47         Object out;
48         ObjectMapper mapper = threadLocal.get();
49         response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
50         if (body instanceof Result) {
51             out = body;
52         } else if (body instanceof String) {
53             Result<Object> result = Result.success(body);
54             try {
55                 out = mapper.writeValueAsString(result);
56             } catch (JsonProcessingException e) {
57                 out = Result.failure();
58             }
59         } else {
60             out = Result.success(body);
61         }
62         return out;
63 
64     }
65 
66     private boolean validateMethod(MethodParameter methodParameter) {
67         AnnotatedElement element = methodParameter.getAnnotatedElement();
68         return Arrays.stream(annos).anyMatch(anno -> anno.isAnnotation() && element.isAnnotationPresent(anno));
69     }
70 71 }

还记得上边有翻译或supports方法的注释吗?此组件是否支持给定的控制器方法返回类型和所选的{@code HttpMessageConverter}类型,但是我们在这里并不关心后半部分,是不是HttpMessageConverter类型在封装json的时候框架本身回去验证(AbstractMappingJacksonResponseBodyAdvice.java)我们所关心的应该是,这个方法是不是@RequestMapping标记的,所以我只验证了这一点。

当然,我这里用的自定义注解,没有什么特殊的需求可以直接拦截@RestController即可

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/11/5 8:42
 */
@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ResultBody {
}

接下来就是测试了:

 

 

 

 完美(当然期间遇到很多坑,差点就死了)

欢迎大佬吐槽

posted @ 2020-11-10 10:08  葬月!  阅读(1641)  评论(3编辑  收藏  举报