简单拦截器日志打印两种实现
AOP拦截
package com
.per
.demo
.configuration
;
import com
.alibaba
.fastjson
.JSONObject
;
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
.stereotype
.Component
;
import org
.springframework
.web
.context
.request
.RequestContextHolder
;
import org
.springframework
.web
.context
.request
.ServletRequestAttributes
;
import org
.springframework
.web
.servlet
.HandlerInterceptor
;
import javax
.servlet
.http
.HttpServletRequest
;
import java
.util
.Arrays
;
import java
.util
.Enumeration
;
@Aspect
@Component
public class LoggerConfiguration{
protected final Logger logger
= LoggerFactory
.getLogger(this.getClass());
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) " +
"|| @annotation(org.springframework.web.bind.annotation.PostMapping) " +
"||@annotation(org.springframework.web.bind.annotation.GetMapping)")
public void controllerLog() {
}
@Around("controllerLog()")
public Object
interception(ProceedingJoinPoint joinPoint
) throws Throwable
{
ServletRequestAttributes attributes
= (ServletRequestAttributes
) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request
= attributes
.getRequest();
Long startTime
= System
.currentTimeMillis();
Enumeration
<String> paramNames
= request
.getParameterNames();
JSONObject paramJson
= new JSONObject();
while (paramNames
.hasMoreElements()) {
String paramName
= paramNames
.nextElement();
paramJson
.put(paramName
, request
.getParameter(paramName
));
}
StringBuilder param
= new StringBuilder();
if (joinPoint
.getArgs() != null
) {
Arrays
.asList(joinPoint
.getArgs()).forEach(obj
-> param
.append(",").append(obj
));
}
Object result
= null
;
Throwable exception
= null
;
try {
result
= joinPoint
.proceed();
} catch (Throwable throwable
) {
exception
= throwable
;
throw throwable
;
} finally {
logger
.info(request
.getMethod() + " "
+ request
.getRequestURL() + " 参数:" + paramJson
.toJSONString() + param
+ " ," + (System
.currentTimeMillis() - startTime
) + "ms," + " 响应结果:" + result
, exception
);
}
return result
;
}
}
实现HandlerInterceptor
package com
.per
.demo
.configuration
;
import org
.springframework
.web
.servlet
.HandlerInterceptor
;
import org
.springframework
.web
.servlet
.ModelAndView
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
public class LoggerConfiguration1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request
, HttpServletResponse response
, Object handler
) throws Exception
{
return false;
}
@Override
public void postHandle(HttpServletRequest request
, HttpServletResponse response
, Object handler
, ModelAndView modelAndView
) throws Exception
{
}
@Override
public void afterCompletion(HttpServletRequest request
, HttpServletResponse response
, Object handler
, Exception ex
) throws Exception
{
}
}
在三个方法针对自己想处理的先后,写上自己代码就ok
最后说一下aop这几个注解的先后顺序
Aound ->before->proceed->after->afterReturning->afterThrowing
正常情况
异常情况