溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

SpringBoot打印POST請(qǐng)求原始入?yún)ody體的過(guò)程

發(fā)布時(shí)間:2021-09-13 09:09:57 來(lái)源:億速云 閱讀:405 作者:chen 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“SpringBoot打印POST請(qǐng)求原始入?yún)ody體的過(guò)程”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“SpringBoot打印POST請(qǐng)求原始入?yún)ody體的過(guò)程”吧!

目錄
  • SpringBoot打印POST請(qǐng)求原始入?yún)ody體

    • 1、首先定義過(guò)濾器配置

    • 2、實(shí)現(xiàn)1中的過(guò)濾器

  • Post接收不到body里的參數(shù)(對(duì)象參數(shù))

    • 檢查注解

    • 檢查實(shí)體

    • 檢查Content-Type

SpringBoot打印POST請(qǐng)求原始入?yún)ody體

1、首先定義過(guò)濾器配置

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、實(shí)現(xiàn)1中的過(guò)濾器

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();
    }
 
    /**
     * 打印請(qǐng)求參數(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 "";
    }
 
    /**
     * 獲取請(qǐng)求地址上的參數(shù)
     *
     * @param request
     * @return
     */
    public static String getRequestParams(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        Enumeration<String> enu = request.getParameterNames();
        //獲取請(qǐng)求參數(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ù)(對(duì)象參數(shù))

檢查注解

  • @ResponseBody

  • @RequestBody

SpringBoot打印POST請(qǐng)求原始入?yún)ody體的過(guò)程

檢查實(shí)體

接收實(shí)體類(lèi),set、get方法是否正確

檢查Content-Type

是否是application/json

到此,相信大家對(duì)“SpringBoot打印POST請(qǐng)求原始入?yún)ody體的過(guò)程”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI