C语言学习网

怎么在Spring MVC中打印@RequestBody、@Response日志

发表于:2022-11-27 作者:安全数据网编辑
编辑最后更新 2022年11月27日,今天就跟大家聊聊有关怎么在Spring MVC中打印@RequestBody、@Response日志,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收

今天就跟大家聊聊有关怎么在Spring MVC中打印@RequestBody、@Response日志,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

可以通过自定义RequestBodyAdvisor、ResponseBodyAdvisor来实现日志输出。

  • RequestBodyAdvisor可以获取到解析后的Controller方法参数对象。

  • ResponseBodyAdvisor可以获取到Controller方法返回值对象。

然后将他们注册到requestMappingHandlerAdapter:

// 继承WebMvcConfigurationSupport, 重写该方法@Override@Beanpublic RequestMappingHandlerAdapter requestMappingHandlerAdapter() {  RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter();  adapter.setRequestBodyAdvice(Lists.newArrayList(new CustomerRequestBodyAdvisor()));  adapter.setResponseBodyAdvice(Lists.newArrayList(new CustomerResponseBodyAdvisor()));  return adapter;}

另附CustomerRequestBodyAdvisor、CustomerResponseBodyAdvisor日志输出实现参考:

RequestBodyAdvisor实现参考:

// CustomerRequestBodyAdvisor.java/*** 打印请求参数日志*/public class CustomerRequestBodyAdvisor extends RequestBodyAdviceAdapter {  private static final Logger logger = LoggerFactory.getLogger(CustomerRequestBodyAdvisor.class);  @Override  public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) {    // 只处理@RequestBody注解了的参数    return methodParameter.getParameterAnnotation(RequestBody.class) != null;  }  @Override  public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) {    Method method = parameter.getMethod();        // 参数对象转JSON字符串    String jsonBody;    if (StringHttpMessageConverter.class.isAssignableFrom(converterType)) {      jsonBody = body.toString();    } else {      jsonBody = JSON.toJSONString(body, SerializerFeature.UseSingleQuotes);    }        // 自定义日志输出    if (logger.isInfoEnabled()) {      logger.info("{}#{}: {}", parameter.getContainingClass().getSimpleName(), method.getName(), jsonBody);      //      logger.info("json request<=========method:{}#{}", parameter.getContainingClass().getSimpleName(), method.getName());    }    return super.afterBodyRead(body, inputMessage, parameter, targetType, converterType);  }}

ResponseBodyAdvisor实现参考:

// CustomerResponseBodyAdvisor.java/*** 打印响应值日志*/public class CustomerResponseBodyAdvisor implements ResponseBodyAdvice {  private static final Logger logger = LoggerFactory.getLogger(CustomerResponseBodyAdvisor.class);  @Override  public boolean supports(MethodParameter returnType, Class> converterType) {    return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType)        || returnType.getMethod().isAnnotationPresent(ResponseBody.class);  }  @Override  public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {    // 响应值转JSON串输出到日志系统    if (logger.isInfoEnabled()) {      logger.info("{}: {}", request.getURI(), JSON.toJSONString(body, SerializerFeature.UseSingleQuotes));    }    return body;  }}

看完上述内容,你们对怎么在Spring MVC中打印@RequestBody、@Response日志有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0