1,请求参数的打印
@ControllerAdvice(basePackages = "控制器的命名空间")
public class LogRequestAdvice implements RequestBodyAdvice {
Logger logger = LoggerFactory.getLogger(getClass());
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//判断是否有此注解
boolean b = methodParameter.getParameterAnnotation(RequestBody.class) != null;
//只有为true时才会执行afterBodyRead
return b;
}
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
InputStream body = httpInputMessage.getBody();
return new MappingJacksonInputMessage(httpInputMessage.getBody(), httpInputMessage.getHeaders());
}
@Override
public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//仅仅打印请求参数
String s = JSON.toJSONString(o);
/**
* 去除注 回车 \n 水平制表符 \t 空格 \s 换行 \r
*/
String s2 = StringUtils.replaceBlankByLow(s);
logger.info("请求参数为:{}", s2);
return o;
}
@Override
public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
return null;
}
2,请求参数的打印
@ControllerAdvice(basePackages = "控制器的命名空间")
return body;
}