springboot aop获取controller的POST @RequestBody注解参数

7 篇文章 0 订阅

项目使用vue+springboot做前后端分离,在做校验的时候需要用到aop拦截

vue

this.$axios.post("http://localhost:8281/store-api/order/test",{
		token:"testtoken"
	}).then(response => {
		console.log(response.data);
	});

vue怎么安装axios自行百度

controller

@CrossOrigin	//为了跨域
@RestController
@RequestMapping("order")
public class OrderApi {
    @Autowired
    OrderService orderService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("test")
    public Object test(@RequestBody JSONObject o){
        logger.info(o.toJSONString());
        return o;
    }
}

我们需要拦截到上面test方法里的o参数

aop

@Aspect
@Configuration
public class CheckAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    // 定义切点Pointcut  自行写入对应的controller包路径
    @Pointcut("execution(* com.ls.store_service.controller.*.*(..))")
    public void excudeService() {
    }

    @Around("excudeService()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();

        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        //这里可以获取到get请求的参数和其他信息
        logger.info("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);
        //重点 这里就是获取@RequestBody参数的关键  调试的情况下 可以看到o变量已经获取到了请求的参数
        Object[] o = pjp.getArgs();

        // result的值就是被拦截方法的返回值
        Object result = pjp.proceed();
        return result;
    }

}

开启调试模式,vue运行发送一个ajax请求,可以看到已经拦截成功并获取到了@RequestBody的参数

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在AOP获取`@RequestBody`的信息,可以使用`JoinPoint`参数和`MethodSignature`对象来获取方法的参数信息。然后,通过遍历参数注解数组,找到带有`@RequestBody`注解参数,并获取相应的信息。 以下是一个示例代码: ```java @Aspect @Component public class RequestBodyAspect { @Before("execution(* com.example.controller.*.*(..))") public void logRequestBody(JoinPoint joinPoint) throws NoSuchMethodException { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String[] parameterNames = signature.getParameterNames(); Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations(); for (int i = 0; i < parameterAnnotations.length; i++) { for (Annotation annotation : parameterAnnotations[i]) { if (annotation instanceof RequestBody) { System.out.println("RequestBody parameter name: " + parameterNames[i]); // 获取参数类型 Class<?> parameterType = signature.getMethod().getParameterTypes()[i]; System.out.println("RequestBody parameter type: " + parameterType); // 其他操作... } } } } } ``` 在上述示例中,切面类`RequestBodyAspect`使用`@Before`注解标记了一个方法`logRequestBody`,该方法会在目标方法执行前执行。通过遍历参数注解数组,找到带有`@RequestBody`注解参数,并获取参数的名称和类型。 请注意,由于`@RequestBody`注解通常用于接收请求体中的JSON或XML等数据,因此参数类型可能是自定义的POJO类或其他类型。您可以根据实际需要进行进一步操作,例如将请求体转换为对象或执行其他逻辑处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值