request获取ajax的url参数,ajax application/json传入后台,拦截器怎么获取参数且不破坏 @RequestBody正常接收?...

当然使用aop来做咯。

代码如下。

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect

@Component

public class LoggingAspect {

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

@Pointcut("within(@org.springframework.stereotype.Repository *)" +

" || within(@org.springframework.stereotype.Service *)" +

" || within(@org.springframework.web.bind.annotation.RestController *)")

public void springBeanPointcut() {

}

@Pointcut("within(com.fanxian.logic.*.repository..*)"+

" || within(com.fanxian.logic.*.service..*)"+

" || within(com.fanxian.logic.*.controller..*)")

public void applicationPackagePointcut() {

}

@AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e")

public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {

log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature().getDeclaringTypeName(),

joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL", e.getMessage(), e);

}

@Around("applicationPackagePointcut() && springBeanPointcut()")

public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {

if (log.isDebugEnabled()) {

log.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(),

joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));

}

try {

Object result = joinPoint.proceed();

if (log.isDebugEnabled()) {

log.debug("Exit: {}.{}() with result = {}", joinPoint.getSignature().getDeclaringTypeName(),

joinPoint.getSignature().getName(), result);

}

return result;

} catch (IllegalArgumentException e) {

log.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()),

joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());

throw e;

}

}

}

springBeanPointcut方法配置了spring注解的切入点,applicationPackagePointcut则为你想要拦截方法的切入点。

logAfterThrowing为拦截捕获到的异常,logAround环绕方法获取到拦截的方法打印方法名,输入的参数等等,再往下的Object result = joinPoint.proceed();为调用目标方法,最后打印了返回值。

你需要的只是获取输入的参数,所以joinPoint.getArgs()就是你需要的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值