SpringBoot打印POST請求原始入?yún)ody體方式
更新時間:2021年09月11日 17:13:16 作者:圍子先生
這篇文章主要介紹了SpringBoot打印POST請求原始入?yún)ody體方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
SpringBoot打印POST請求原始入?yún)ody體
1、首先定義過濾器配置
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;
}
}
2、實現(xiàn)1中的過濾器
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();
}
/**
* 打印請求參數(shù)
*
* @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 "";
}
/**
* 打印返回參數(shù)
*
* @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 "";
}
/**
* 獲取請求地址上的參數(shù)
*
* @param request
* @return
*/
public static String getRequestParams(HttpServletRequest request) {
StringBuilder sb = new StringBuilder();
Enumeration<String> enu = request.getParameterNames();
//獲取請求參數(shù)
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();
}
}
Post接收不到body里的參數(shù)(對象參數(shù))
檢查注解
@ResponseBody@RequestBody

檢查實體
接收實體類,set、get方法是否正確
檢查Content-Type
是否是application/json
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Web實現(xiàn)session過期后自動跳轉(zhuǎn)到登陸頁功能【基于過濾器】
這篇文章主要介紹了Java Web實現(xiàn)session過期后自動跳轉(zhuǎn)到登陸頁功能,涉及java過濾器針對session的判斷與跳轉(zhuǎn)相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
java使用泛型實現(xiàn)棧結(jié)構(gòu)示例分享
泛型是Java SE5.0的重要特性,使用泛型編程可以使代碼獲得最大的重用。由于在使用泛型時要指明泛型的具體類型,這樣就避免了類型轉(zhuǎn)換。本實例將使用泛型來實現(xiàn)一個棧結(jié)構(gòu),并對其進行測試2014-03-03
springboot tomcat最大線程數(shù)與最大連接數(shù)解析
這篇文章主要介紹了springboot tomcat最大線程數(shù)與最大連接數(shù)解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
spring 中事務(wù)注解@Transactional與trycatch的使用
這篇文章主要介紹了spring 中事務(wù)注解@Transactional與trycatch的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

