今天就跟大家聊聊有關怎么在Spring MVC中打印@RequestBody、@Response日志,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)專注于恩平網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經驗。 熱誠為您提供恩平營銷型網(wǎng)站建設,恩平網(wǎng)站制作、恩平網(wǎng)頁設計、恩平網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務,打造恩平網(wǎng)絡公司原創(chuàng)品牌,更為您提供恩平網(wǎng)站排名全網(wǎng)營銷落地服務。
可以通過自定義RequestBodyAdvisor、ResponseBodyAdvisor來實現(xiàn)日志輸出。
RequestBodyAdvisor可以獲取到解析后的Controller方法參數(shù)對象。
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日志輸出實現(xiàn)參考:
RequestBodyAdvisor實現(xiàn)參考:
// CustomerRequestBodyAdvisor.java /** * 打印請求參數(shù)日志 */ 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注解了的參數(shù) 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(); // 參數(shù)對象轉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實現(xiàn)參考:
// 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串輸出到日志系統(tǒng) if (logger.isInfoEnabled()) { logger.info("{}: {}", request.getURI(), JSON.toJSONString(body, SerializerFeature.UseSingleQuotes)); } return body; } }
看完上述內容,你們對怎么在Spring MVC中打印@RequestBody、@Response日志有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)頁名稱:怎么在SpringMVC中打印@RequestBody、@Response日志
網(wǎng)頁鏈接:http://aaarwkj.com/article20/psodjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、品牌網(wǎng)站制作、ChatGPT、網(wǎng)站營銷、搜索引擎優(yōu)化、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)