Browse Source

/、.test

release_toaliyun_real
xhxu 4 years ago
parent
commit
4247b2d4fb
1 changed files with 68 additions and 0 deletions
  1. +68
    -0
      mallinkBApi/src/main/java/com/iformall/interceptor/TestControllerReturnAspect.java

+ 68
- 0
mallinkBApi/src/main/java/com/iformall/interceptor/TestControllerReturnAspect.java View File

@@ -0,0 +1,68 @@
package com.iformall.interceptor;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;


/**
* 测试所有controler请求返回信息输出
* @author alascor
*
*/
@Aspect
@Configuration
@Slf4j
public class TestControllerReturnAspect {

@Pointcut("execution(* com.iformall.controller.*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 method = request.getMethod();
String uri = request.getRequestURI();
String paraString = JSON.toJSONString(request.getParameterMap());
String requestBody = getBodyRequest(request);
//log.info("***************************************************");
//log.info("请求开始, URI: {}, method: {}, params: {}", uri, method, paraString);
// result的值就是被拦截方法的返回值
Object result = pjp.proceed();
log.info("开发调试请求结果日志,生产请关闭!!!!>>>>>>>>请求结束,URI: {}, method: {}, params: {},requestBody:{}. 返回值:{} " ,uri, method, paraString,requestBody, JSON.toJSONString(result));
return result;
}
private String getBodyRequest(HttpServletRequest request) throws IOException {
// 直接从HttpServletRequest的Reader流中获取RequestBody
BufferedReader reader = request.getReader();
StringBuilder builder = new StringBuilder();
String line = reader.readLine();
while(line != null){
builder.append(line);
line = reader.readLine();
}
reader.close();

String reqBody = builder.toString();
return reqBody;
}
}

Loading…
Cancel
Save