Spring框架接口之RequestBodyAdvice和ResponseBodyAdvice

Spring框架接口之RequestBodyAdvice和ResponseBodyAdvice

Spring框架接口之 RequestBodyAdvice 和 ResponseBodyAdvice

RequestBodyAdviceResponseBodyAdvice 是Spring MVC 中两个非常重要的高级特性,于Spring 4.2 版本引入,它们提供了在消息转换器(HttpMessageConverter) 执行前后进行拦截的切面能力,便于对请求体和响应体进行全局性的、非侵入式的处理。下面将详细介绍一下这两个接口。


RequestBodyAdvice

RequestBodyAdvice 用于在读取请求体之前(或读取得到的对象作为参数传入 @RequestBody 或 HttpEntity Controller 方法之前)进行自定义处理。

1、核心方法

方法 说明
boolean supports(...) 判断该 Advice 是否支持当前的请求(根据方法参数、注解类型等)。
HttpInputMessage beforeBodyRead(...) 在 body 被读取之前 调用。可以用于包装或替换原始的 HttpInputMessage(例如,对加密的请求体进行解密)。
Object afterBodyRead(...) 在 body 被读取并转换之后调用。可以对转换得到的 Java 对象进行后处理(例如,数据校验、属性填充)。
Object handleEmptyBody(...) 当请求体为空时调用。可以返回一个默认值。

2、典型应用场景

  1. 全局请求数据解密:客户端发送的数据是加密的,在 Controller 接收到明文对象之前,先在这里进行解密。

  2. 请求日志记录:记录下请求体的原始内容,用于调试或审计。

  3. 预处理或数据包装:将原始的请求数据包装成一个特定的上下文对象,再传递给 Controller。

  4. 默认值处理:当请求体为空时,提供一个默认的请求对象。


3、工作流程

HttpRequest -> beforeBodyRead (可操作原始流) -> HttpMessageConverter (转换流为对象) -> afterBodyRead (可操作转换后的对象) -> @RequestBody 参数


ResponseBodyAdvice

ResponseBodyAdvice 用于在执行完 @ResponseBodyResponseEntity 控制器方法后,响应体被处理之前对 Controller 方法值进行自定义处理。

1、核心方法

方法 说明
boolean supports(...) 判断该 Advice 是否支持当前的响应(根据返回值类型、方法注解等。
T beforeBodyWrite(...) 在 body 被写入响应之前调用。这是对返回体进行处理的主要方法,可以修改或替换返回的对象。

2、典型应用场景

  1. 统一响应体封装:这是最经典的用法。将 Controller 返回的各种类型(String, Object, List 等)统一包装成 Result/ApiResponse 格式(如 {code: 200, data: ..., message: "success"})。

  2. 全局响应数据加密:在响应写出到客户端之前,对数据进行加密。

  3. 响应日志记录:记录即将返回给客户端的响应体。

  4. 敏感信息过滤:在响应写出前,脱敏或过滤掉敏感数据(如密码、手机号)。


3、工作流程

Controller 返回值 -> beforeBodyWrite (可操作返回对象) -> HttpMessageConverter (将对象转换为流) -> HttpResponse


代码实践

1、使用 RequestBodyAdvice 进行请求解密

java 复制代码
@ControllerAdvice
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {

    @Autowired
    private DecryptService decryptService; // 假设的解密服务

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        // 只处理带有 @Decrypt 注解的参数
        return methodParameter.hasParameterAnnotation(Decrypt.class);
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                          Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        // 1. 读取加密的请求体
        String encryptedBody = StreamUtils.copyToString(inputMessage.getBody(), StandardCharsets.UTF_8);

        // 2. 进行解密
        String decryptedBody = decryptService.decrypt(encryptedBody);

        // 3. 将解密后的字符串重新封装成新的 InputMessage 并返回
        return new ByteArrayHttpInputMessage(decryptedBody.getBytes(), inputMessage.getHeaders());
    }

    // 其他方法可以默认实现...
    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                               Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

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

    // 内部类,用于创建新的 HttpInputMessage
    static class ByteArrayHttpInputMessage implements HttpInputMessage {
        private final byte[] body;
        private final HttpHeaders headers;

        public ByteArrayHttpInputMessage(byte[] body, HttpHeaders headers) {
            this.body = body;
            this.headers = headers;
        }

        @Override
        public InputStream getBody() {
            return new ByteArrayInputStream(body);
        }

        @Override
        public HttpHeaders getHeaders() {
            return headers;
        }
    }
}

2、使用 ResponseBodyAdvice 实现统一响应包装

定义一个统一响应类

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private Integer code;
    private String message;
    private T data;

    public static <T> Result<T> success(T data) {
        return new Result<>(200, "成功", data);
    }
}
java 复制代码
// 使用 @ControllerAdvice 注解,使其成为一个全局组件
@RestControllerAdvice(basePackages = "com.example.controller")
public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {

    /**
     * 判断是否支持增强处理。
     * 这里我们排除掉本身就是 Result 类型的返回和一些特殊类型(如 String,需要单独处理)。
     */
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        // 检查方法上是否有不需要包装的注解(自定义)
        // 如果方法或类上有 @IgnoreResponseAdvice 注解,则不做处理
        if (returnType.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)
                || returnType.getContainingClass().isAnnotationPresent(IgnoreResponseAdvice.class)) {
            return false;
        }
        return true;
    }

    /**
     * 在写入响应体之前进行处理
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

        // 如果返回体已经是 Result 类型,则直接返回(避免重复包装)
        if (body instanceof Result) {
            return body;
        }
        // 如果返回的是 String 类型,需要特殊处理
        // 因为 StringHttpMessageConverter 会直接写入字符串,不会转成JSON
        if (body instanceof String) {
            // 通常需要手动将 Result 对象序列化成 JSON 字符串
            // 这里需要确保 Jackson 等库在类路径上
            try {
                return new ObjectMapper().writeValueAsString(Result.success(body));
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }

        // 最普遍的情况:将原始数据包装成 Result 对象
        return Result.success(body);
    }
}

补充

RequestBodyAdvice 接口有实现类 RequestBodyAdviceAdapter (抽象类),在使用时,如 Interceptor (拦截器)处理请求时可以既继承 RequestBodyAdviceAdapter类,又实现 ResponseBodyAdvice 接口,实现对请求前后同时处理。

相关推荐
yeshihouhou3 小时前
tomcat
java·tomcat
吾当每日三饮五升3 小时前
RapidJSON 自定义内存分配器详解与实战
c++·后端·性能优化·json
Seven973 小时前
Java 日志管理的黄金组合: SLF4J+Logback
java
shark_chili3 小时前
Linux IO性能瓶颈排查全攻略:从理论到实战的系统性解决方案
后端
喵手3 小时前
Java中的大数据流式计算与Apache Kafka集成!
java·华为云·apache
卷福同学3 小时前
#去深圳了~
后端·面试·架构
在下村刘湘3 小时前
Maven setting文件中<mirrors>(镜像)和 <servers>两个标签的区别
java·maven
CodeSheep3 小时前
稚晖君公司最新合伙人,公开了!
前端·后端·程序员
fatfishccc3 小时前
(四)优雅重构:洞悉“搬移特性”的艺术与实践
java·驱动开发·intellij-idea·软件研发·后端开发·代码重构·搬移