设为首页 加入收藏

TOP

java 拦截、过滤器2(四)
2023-07-26 08:17:45 】 浏览:158
Tags:java 拦截
ver(一般实际为RequestResponseBodyMethodProcessor): // 唯一构造函数,指定所有的advices public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters, @Nullable List<Object> requestResponseBodyAdvice) { Assert.notEmpty(converters, "'messageConverters' must not be empty"); this.messageConverters = converters; this.allSupportedMediaTypes = getAllSupportedMediaTypes(converters); this.advice = new RequestResponseBodyAdviceChain(requestResponseBodyAdvice); }

此构造函数在new RequestResponseBodyMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice)时候调用,传进来的requestResponseBodyAdvice就刚好是在初始化RequestMappingHandlerAdapter的时候全局扫描进来的所有的增强器们

3.2 如何使用

请求日志的打印,用于POST请求的,这里实现了RequestBodyAdvice用来打印请求参数,也使用了ResponseBodyAdvice打印返回的信息

// 生成日志信息,并放到request中
@ControllerAdvice
public class RequestBodyAdviceHandler implements RequestBodyAdvice {
    public RequestBodyAdviceHandler() {
    }

    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        Method method = methodParameter.getMethod();
        Class<?> declaringClass = method.getDeclaringClass();
        RestController RestController = (RestController)declaringClass.getAnnotation(RestController.class);
        return RestController != null;
    }

    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        return inputMessage;
    }

    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        this.writeRequestLog(body, inputMessage, parameter, targetType, converterType);
        return body;
    }

    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        this.writeRequestLog(body, inputMessage, parameter, targetType, converterType);
        return body;
    }

    private String toJSONString(Object body, MethodParameter parameter) {
        IgnoreLogBody ignore = (IgnoreLogBody)parameter.getMethodAnnotation(IgnoreLogBody.class);
        if (ignore == null) {
            return JSON.toJSONString(body);
        } else {
            String[] ignoreKey = ignore.ignoreKey();
            return ignoreKey != null && ignoreKey.length != 0 ? JSON.toJSONString(body, new IgnoreLogPropertyFilter(ignore.ignoreKey(), ignore.key()), new SerializerFeature[0]) : JSON.toJSONString(body);
        }
    }

    private void writeRequestLog(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        HttpServletRequest request = RequestHelper.getRequest();
        request.setAttribute("_REQUEST_STARTTIME_", System.currentTimeMillis());
        String requestId = request.getHeader("_REQUEST_ID_");
        if (StringUtils.isEmptyStr(requestId)) {
            requestId = TraceContext.traceId();
        }

        if (StringUtils.isEmptyStr(requestId) || "Ignored_Trace".equals(requestId)) {
            requestId = UUID.randomUUID().toString().r
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 4/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MyBatis 常用工具类 下一篇Java并发工具CountDownLatch的使..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目