当前位置: 首页> 健康> 知识 > SpringBoot 实现RequestBodyAdvice封装统一接受类功能

SpringBoot 实现RequestBodyAdvice封装统一接受类功能

时间:2025/7/14 5:02:29来源:https://blog.csdn.net/weixin_49171365/article/details/139854326 浏览次数:0次

一、相关往期文章

SpringBoot+Vue实现AOP系统日志功能_aop的vue完整项目

Spring AOP (面向切面编程)原理与代理模式—实例演示_面向切面aop原理详解

二、需求分析

按照一般情况,统一接受类可以像以下的方式进行处理:

        如果不想使用 @RequestBody RequestPack<RequestPackSave> requestPack  当然也可以使用AOP在controller层数据执行之前,对数据进行处理。

三、代码实现

controller层正常写就行

//该方法只对使用了@RequestBody注解的参数生效
@RestControllerAdvice
public class GlobalRequestAdvice implements RequestBodyAdvice {@Overridepublic boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {// 此处true代表执行当前advice的业务,false代表不执行return true;}/*** 读取参数前执行** @param httpInputMessage* @param methodParameter* @param type* @param aClass* @return 返回一个新的 HttpInputMessage,该消息可能包含修改后的请求体。* @throws IOException* HttpInputMessage httpInputMessage: 表示原始的 HTTP 请求消息。* MethodParameter methodParameter: 表示控制器方法的参数。* Type type: 表示方法参数的类型。* Class<? extends HttpMessageConverter<?>> aClass: 表示将要使用的* HttpMessageConverter 类型。*/@Overridepublic HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {return new HttpInputMessage() {@Overridepublic InputStream getBody() throws IOException {String bodyStr = IOUtils.toString(httpInputMessage.getBody(), "utf-8");GlobalHttpReceive httpReceive = GsonUtil.jsonToObject(bodyStr, GlobalHttpReceive.class);if (httpReceive != null) {Object data = httpReceive.getReqData();if (data != null) {return IOUtils.toInputStream(GsonUtil.objectToJson(data), "utf-8");}}throw new SSError(GlobalCodeEnum.RequestFormatError);}@Overridepublic HttpHeaders getHeaders() {return httpInputMessage.getHeaders();}};}/*** 读取参数后执行** @param o* @param httpInputMessage* @param methodParameter* @param type* @param aClass* @return*/@Overridepublic Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {return o;}/*** 无请求时的处理** @param o* @param httpInputMessage* @param methodParameter* @param type* @param aClass* @return*/@Overridepublic Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {return o;}
}

四、解释说明

  • RequestBodyAdvice 的设计初衷是为了拦截并处理那些使用了 @RequestBody 注解的参数。也就是说,当一个控制器方法参数被 @RequestBody 注解标注时,Spring 会在处理该参数时调用 RequestBodyAdvice
  • supports 方法的参数MethodParameter methodParameter: 代表方法参数的相关信息。Type type: 代表方法参数的类型。Class<? extends HttpMessageConverter<?>> aClass: 代表将要使用的 HttpMessageConverter 类型。
  • 始终返回 true:

    在实现 RequestBodyAdvice 接口的类中,supports 方法返回 true 意味着对于所有进入的请求体参数,都会执行 beforeBodyReadafterBodyRead 和 handleEmptyBody 等方法。换句话说,无论请求体的内容是什么,只要这个参数被 @RequestBody 注解标记,当前的 RequestBodyAdvice 实现就会对其进行处理。
  • 当 Spring 处理某个带有 @RequestBody 注解的方法参数时,会调用 supports 方法。
  • 由于 supports 方法始终返回 true,因此所有带有 @RequestBody 注解的参数都会被当前的 RequestBodyAdvice 处理。
return new HttpInputMessage() {@Overridepublic InputStream getBody() throws IOException {String bodyStr = IOUtils.toString(httpInputMessage.getBody(), "utf-8");GlobalHttpReceive httpReceive = GsonUtil.jsonToObject(bodyStr, GlobalHttpReceive.class);if (httpReceive != null) {Object data = httpReceive.getReqData();if (data != null) {return IOUtils.toInputStream(GsonUtil.objectToJson(data), "utf-8");}}throw new SSError(GlobalCodeEnum.RequestFormatError);}@Overridepublic HttpHeaders getHeaders() {return httpInputMessage.getHeaders();}
};

这段代码是一个全局请求处理的拦截器类,主要实现了Spring的RequestBodyAdvice接口,用于对请求的@RequestBody参数进行处理。具体功能如下:

  1. supports方法用于判断是否执行当前advice的业务逻辑,这里始终返回true,表示对所有使用了@RequestBody注解的参数进行处理。

  2. beforeBodyRead方法在读取参数前执行,首先将请求体内容转换为字符串,然后尝试将其转换为GlobalHttpReceive对象,提取其中的reqData字段。如果reqData不为空,则将其转换为JSON字符串后重新封装成InputStream返回;否则抛出SSError(GlobalCodeEnum.RequestFormatError)异常。

  3. afterBodyRead方法在读取参数后执行,这里直接返回参数对象。

  4. handleEmptyBody方法用于处理无请求体时的情况,这里也直接返回参数对象。

        通过始终返回 truesupports 方法确保了任何带有 @RequestBody 注解的参数都会进入 RequestBodyAdvice 的处理方法中,从而实现对这些参数的统一预处理逻辑。这种设计使得开发者可以集中管理和处理所有请求体的数据,而不需要在每个控制器方法中分别编写重复的处理代码。

五、对比分析

虽然 RequestBodyAdvice 并不是严格的 AOP,但它确实体现了 AOP 的一些思想。举个例子,如果你要记录所有请求体的数据,可以这样实现:

import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;
import java.lang.reflect.Type;@ControllerAdvice
public class LoggingRequestBodyAdvice extends RequestBodyAdviceAdapter {@Overridepublic boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {// 支持所有请求体的处理return true;}@Overridepublic Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {// 在读取请求体之后记录日志System.out.println("Request Body: " + body);return body;}@Overridepublic Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {// 处理空请求体的情况System.out.println("Request Body is empty");return body;}
}

  RequestBodyAdvice 并不完全属于 AOP 范畴,但它利用了类似于 AOP 的拦截机制来处理请求体的读取过程。在 Spring Boot 中,真正的 AOP 通常通过 @Aspect 注解和切点表达式来实现,用于更广泛的应用场景。而 RequestBodyAdvice 则是专门针对 HTTP 请求体处理的一种机制。

关键字:SpringBoot 实现RequestBodyAdvice封装统一接受类功能

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: