在无法使用spring提供的增强方法时,可以自定义实现打印request body入参
@Aspect
@Component
@Slf4j
public class ControllerAspect {
private final HttpServletRequest request;
public ControllerAspect(HttpServletRequest request) {
this.request = request;
}
@Pointcut("execution(* com.hst.project.sceneweb.web.controller.*Controller*.*(..))")
public void controllerAspect() {
}
@Around(value = "controllerAspect()", argNames = "pjp")
private AjaxResult doAround(ProceedingJoinPoint pjp) throws Exception {
try {
String method = request.getMethod();
Object[] args = pjp.getArgs();
String params = "";
//获取请求参数集合并进行遍历拼接
if (args.length > 0) {
if (!"GET".equals(method)) {
log.info("请求query入参===" + request.getQueryString());
Object object = args[0];
Map map = getKeyAndValue(object);
params = JSON.toJSONString(map);
log.info("请求body 入参===" + params);
} else {
log.info("请求query入参===" + request.getQueryString());
}
}
// result的值就是被拦截方法的返回值
Object result = pjp.proceed();
AjaxResult ajaxResult = (AjaxResult) result;
if (BeanUtil.checkIsNotEmpty(ajaxResult)) {
Params ajaxResultParams = new Params();
if (BeanUtil.checkIsNotEmpty(request.getQueryString())) {
ajaxResultParams.setQueryParams(URLDecoder.decode(request.getQueryString(), "UTF-8"));
}
ajaxResultParams.setBodyParams(JSON.parseObject(params));
ajaxResult.setParams(ajaxResultParams);
log.info("请求出参 ===" + ajaxResult.toString());
}
return (AjaxResult) result;
} catch (Throwable throwable) {
if (throwable instanceof ParamException) {
throw new ParamException(throwable.getMessage());
} else if (throwable instanceof HttpException) {
throw new HttpException(((HttpException) throwable).getCode(), throwable.getMessage());
} else if (throwable instanceof ServiceException) {
throw new ServiceException(throwable.getMessage());
} else if (throwable instanceof InvokeException) {
throw new InvokeException(throwable.getMessage());
} else {
throw new Exception(throwable);
}
}
}
private static Map<String, Object> getKeyAndValue(Object obj) {
Map<String, Object> map = Maps.newHashMapWithExpectedSize(10);
// 得到类对象
Class userCla = obj.getClass();
/* 得到类中的所有属性集合 */
Field[] fs = userCla.getDeclaredFields();
for (Field f : fs) {
// 设置些属性是可以访问的
f.setAccessible(true);
Object val;
try {
// 得到此属性的值
val = f.get(obj);
// 设置键值
map.put(f.getName(), val);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
return map;
}