HandlerInterceptor

  1. public interface HandlerInterceptor {
  2. /**
  3. * 预处理回调方法,实现处理器的预处理(如检查登陆),第三个参数为响应的处理器,自定义Controller
  4. * 返回值:true表示继续流程(如调用下一个拦截器或处理器);   * false表示流程中断(如登录检查失败),不会继续调用其他的拦截器或处理器,此时我们需要通过response来产生响应;
  5. */
  6. boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
  7. throws Exception;
  8. /**
  9. * 后处理回调方法,实现处理器的后处理(但在渲染视图之前),此时我们可以通过modelAndView(模型和视图对象)对模型数据进行处理或对视图进行处理,modelAndView也可能为null。
  10. */
  11. void postHandle(
  12. HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
  13. throws Exception;
  14. /**
  15. * 整个请求处理完毕回调方法,即在视图渲染完毕时回调,如性能监控中我们可以在此记录结束时间并输出消耗时间,还可以进行一些资源清理,类似于try-catch-finally中的finall
  16.    * 但仅调用处理器执行链中preHandle返回true的拦截器的afterCompletion。
  17. */
  18. void afterCompletion(
  19. HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
  20. throws Exception;
  21. }

ResponseBodyAdvice

一、接口源码

public interface ResponseBodyAdvice<T>{
  /**
     * 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(返回是否调用处理方法)
     */
    boolean support(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);
/**
     * Invoked after an {@code HttpMessageConverter} is selected and just before
     * its write method is invoked.
     * @param body the body to be written(需要写操作的body)
     * @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
    T beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType,
            ServerHttpRequest request, ServerHttpResponse response);
}

二、作用范围

ResponseBodyAdvice可以在注解@ResponseBody将返回值处理成相应格式之前操作返回值。
实现这个接口即可完成相应操作。可用于返回值加密

三、方法解析

1、boolean supports

boolean support(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);

该方法返回值true或者false用于返回当前处理方法是否需要执行

2、T beforeBodyWrite

Object beforeBodyWrite(Object body, MethodParameter returnType,
            MediaType selectedContentType, Class selectedConverterType,
            ServerHttpRequest request, ServerHttpResponse response)

该方法使用对响应结果的处理,返回的结果类型包装,包含异常的响应结果

  1. Object body 是控制器返回的处理结果
  2. MethodParameter returnType 是方法的相关参数
  3. MediaType selectedContentType 是参数ContentType
  4. Class selectedConverterType 转换器类型
  5. ServerHttpRequest request 请求
  6. ServerHttpResponse response 响应

    四、使用方式

  • 在类上直接加上@ControllerAdvice注解
  • 注册到RequestMappingHandlerAdapter中