您好,登錄后才能下訂單哦!
本篇內容主要講解“SpringBoot打印POST請求原始入參body體的過程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“SpringBoot打印POST請求原始入參body體的過程”吧!
SpringBoot打印POST請求原始入參body體
1、首先定義過濾器配置
2、實現1中的過濾器
Post接收不到body里的參數(對象參數)
檢查注解
檢查實體
檢查Content-Type
package com.choice.o2o.device.common.config; import com.choice.o2o.device.common.filter.LogFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FilterConfig { @Bean public FilterRegistrationBean registFilter() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new LogFilter()); registration.addUrlPatterns("/*"); registration.setName("LogFilter"); registration.setOrder(1); return registration; } }
package com.choice.o2o.three.code.config.log; import lombok.extern.slf4j.Slf4j; import org.springframework.core.Ordered; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.ContentCachingRequestWrapper; import org.springframework.web.util.ContentCachingResponseWrapper; import org.springframework.web.util.WebUtils; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Enumeration; @Slf4j public class LogParamFilter extends OncePerRequestFilter implements Ordered { // put filter at the end of all other filters to make sure we are processing after all others private int order = Ordered.LOWEST_PRECEDENCE - 8; public static final String SPLIT_STRING_M = "="; public static final String SPLIT_STRING_DOT = ", "; @Override public int getOrder() { return order; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { ContentCachingRequestWrapper wrapperRequest = new ContentCachingRequestWrapper(request); ContentCachingResponseWrapper wrapperResponse = new ContentCachingResponseWrapper(response); String urlParams = getRequestParams(request); filterChain.doFilter(wrapperRequest, wrapperResponse); String requestBodyStr = getRequestBody(wrapperRequest); log.info("params[{}] | request body:{}", urlParams, requestBodyStr); String responseBodyStr = getResponseBody(wrapperResponse); log.info("response body:{}", responseBodyStr); wrapperResponse.copyBodyToResponse(); } /** * 打印請求參數 * * @param request */ private String getRequestBody(ContentCachingRequestWrapper request) { ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { String payload; try { payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { payload = "[unknown]"; } return payload.replaceAll("\\n", ""); } } return ""; } /** * 打印返回參數 * * @param response */ private String getResponseBody(ContentCachingResponseWrapper response) { ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { String payload; try { payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { payload = "[unknown]"; } return payload; } } return ""; } /** * 獲取請求地址上的參數 * * @param request * @return */ public static String getRequestParams(HttpServletRequest request) { StringBuilder sb = new StringBuilder(); Enumeration<String> enu = request.getParameterNames(); //獲取請求參數 while (enu.hasMoreElements()) { String name = enu.nextElement(); sb.append(name + SPLIT_STRING_M).append(request.getParameter(name)); if (enu.hasMoreElements()) { sb.append(SPLIT_STRING_DOT); } } return sb.toString(); } }
@ResponseBody
@RequestBody
接收實體類,set、get方法是否正確
是否是application/json
到此,相信大家對“SpringBoot打印POST請求原始入參body體的過程”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。