一、切面把与第三方交互的信息写进ES日志,需要用到环绕增强,但是由于切面出错,竟然把实际业务给影响了,做下笔记,以免后犯。
1、切面中如以下方法,由于要获取请求方法参数,但是未处理参数中有request或者response或者其它的特别请求,在
JSON.toJSONString(“日志写请求参数”)时报错
java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
public Map createOrder(HttpServletRequest request,
@RequestBody String body, @RequestHeader("token") String token) {
2、修改时,我在环绕切面这个大方法直接用try catch全包着,我sb的以为这样就不影响实际业务了,但是报错的地方在调用proceed()方法之前,这样还没执行实际业务就抛错了。
@Around("pointCut()")
public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
try {
long begin = System.currentTimeMillis();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
// IP地址
String ipAddr = getRemoteHost(request);
// 第三方发过来的url其实已经被我们转过了
// String url = request.getRequestURL().toString();
String url = "http://127.0.0.1";
Signature signature = joinPoint.getSignature();
Object obj = joinPoint.getTarget();
MethodSignature methodSignature = (MethodSignature) signature;
Method targetMethod = methodSignature.getMethod();
// 去除参数中有HttpServletRequest或者HttpServletResponse的参数
Object[] args = joinPoint.getArgs();
Stream<?> stream = ArrayUtils.isEmpty(args) ? Stream.empty()