拦截器获取被拦截方法的参数和返回值,并判断传参是否为空

import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Created by xuweilin on 12/16/16.
*/
@Aspect //[color=red]定义一个切面[/color]
@Configuration
public class LogRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class);

// [color=red]定义切点Pointcut,UserController类的所有方法[/color]
@Pointcut("execution(* com.myhexin.xwl.UserController.*(..))")
public void excudeService() {
}

// [color=red]定义切点Pointcut,controller包和所有子包的所有类的方法执行[/color]
@Pointcut("execution(* com.myhexin.xwl.controller..*.*(..))")
public void excudeService2() {
}

//[color=red]表示上面两个都匹配[/color]
@Pointcut("excudeService() || excudeService2()")
public void excudeService3() {
}

@Around("excudeService3()")
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();
logger.info("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);

String res=queryString.replaceAll("%22", "");
logger.info(res);

String[] str=res.split("&");
//[color=red]判断传入的参数是否为空[/color]
for(String str1:str){
String[] str2=str1.split("=");
if(str2.length<=1){
logger.info(str2[0]+"为空!");
break;
}else if(StringUtils.isEmpty(str2[1])){
logger.info(str2[0]+"为空!");
break;
}

}

//[color=red]获取将要执行的方法名称 [/color]
String methodName = pjp.getSignature().getName();
//[color=red]获取执行方法的参数[/color]
Object[] args = pjp.getArgs();
args[args.length-1]=true;
//[color=red]从参数列表中获取参数对象[/color]
for(Object obj : args){//[color=red]查看参数值[/color]
System.out.println("***********"+obj.toString());
}

// [color=red]result的值就是被拦截方法的返回值[/color]
Object result = pjp.proceed();
//[color=red]不返回,被拦截的方法返回值不能被接收(如:前台不能接收到值)[/color]
return result;
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值