今天就跟大家聊聊有关怎么在Spring MVC中打印@RequestBody、@Response日志,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
可以通过自定义RequestBodyAdvisor、ResponseBodyAdvisor来实现日志输出。
RequestBodyAdvisor可以获取到解析后的Controller方法参数对象。
ResponseBodyAdvisor可以获取到Controller方法返回值对象。
然后将他们注册到requestMappingHandlerAdapter:
// 继承WebMvcConfigurationSupport, 重写该方法 @Override @Bean public 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<? extends HttpMessageConverter<?>> converterType) { // 只处理@RequestBody注解了的参数 return methodParameter.getParameterAnnotation(RequestBody.class) != null; } @Override public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> 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<Object> { private static final Logger logger = LoggerFactory.getLogger(CustomerResponseBodyAdvisor.class); @Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) || returnType.getMethod().isAnnotationPresent(ResponseBody.class); } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { // 响应值转JSON串输出到日志系统 if (logger.isInfoEnabled()) { logger.info("{}: {}", request.getURI(), JSON.toJSONString(body, SerializerFeature.UseSingleQuotes)); } return body; } }
看完上述内容,你们对怎么在Spring MVC中打印@RequestBody、@Response日志有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。