您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。