ver(一般实际为RequestResponseBodyMethodProcessor):
// 唯一构造函数,指定所有的advices
public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters, @Nullable List<Object> requestResponseBodyAdvice) {
Assert.notEmpty(converters, "'messageConverters' must not be empty");
this.messageConverters = converters;
this.allSupportedMediaTypes = getAllSupportedMediaTypes(converters);
this.advice = new RequestResponseBodyAdviceChain(requestResponseBodyAdvice);
}
此构造函数在new RequestResponseBodyMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice)时候调用,传进来的requestResponseBodyAdvice就刚好是在初始化RequestMappingHandlerAdapter的时候全局扫描进来的所有的增强器们
3.2 如何使用
请求日志的打印,用于POST请求的,这里实现了RequestBodyAdvice用来打印请求参数,也使用了ResponseBodyAdvice打印返回的信息
// 生成日志信息,并放到request中
@ControllerAdvice
public class RequestBodyAdviceHandler implements RequestBodyAdvice {
public RequestBodyAdviceHandler() {
}
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
Method method = methodParameter.getMethod();
Class<?> declaringClass = method.getDeclaringClass();
RestController RestController = (RestController)declaringClass.getAnnotation(RestController.class);
return RestController != null;
}
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
return inputMessage;
}
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
this.writeRequestLog(body, inputMessage, parameter, targetType, converterType);
return body;
}
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
this.writeRequestLog(body, inputMessage, parameter, targetType, converterType);
return body;
}
private String toJSONString(Object body, MethodParameter parameter) {
IgnoreLogBody ignore = (IgnoreLogBody)parameter.getMethodAnnotation(IgnoreLogBody.class);
if (ignore == null) {
return JSON.toJSONString(body);
} else {
String[] ignoreKey = ignore.ignoreKey();
return ignoreKey != null && ignoreKey.length != 0 ? JSON.toJSONString(body, new IgnoreLogPropertyFilter(ignore.ignoreKey(), ignore.key()), new SerializerFeature[0]) : JSON.toJSONString(body);
}
}
private void writeRequestLog(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
HttpServletRequest request = RequestHelper.getRequest();
request.setAttribute("_REQUEST_STARTTIME_", System.currentTimeMillis());
String requestId = request.getHeader("_REQUEST_ID_");
if (StringUtils.isEmptyStr(requestId)) {
requestId = TraceContext.traceId();
}
if (StringUtils.isEmptyStr(requestId) || "Ignored_Trace".equals(requestId)) {
requestId = UUID.randomUUID().toString().r