<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
import java.util.Map;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import net.sf.json.JSONObject;
@Aspect
@Configuration//定义一个切面
public class LogRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(LogRecordAspect.class);
// 定义切点Pointcut
@Pointcut("execution(* net.xmitech.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();
Object[] args = pjp.getArgs();
String params = "";
//获取请求参数集合并进行遍历拼接
if(args.length>0){
if("POST".equals(method)){
Object object = args[0];
JSONObject json = JSONObject.fromObject(object);
params = json.toString();
;
}else if("GET".equals(method)){
params = queryString;
}
}
logger.info("请求开始===地址:"+url);
logger.info("请求开始===类型:"+method);
logger.info("请求开始===参数:"+params);
// result的值就是被拦截方法的返回值
Object result = pjp.proceed();
logger.info("请求结束===返回值:" + JSONObject.fromObject(result));
return result;
}
}
1、request.getRequestURL()
返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。
2、request.getRequestURI()
得到的是request URL的部分值,并且web容器没有decode过的
3、request.getContextPath()
返回 the context of the request.
4、request.getServletPath()
返回调用servlet的部分url.
5、request.getQueryString()
返回url路径后面的查询字符串