欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot打印POST请求原始入参body体方式

程序员文章站 2022-06-09 20:36:56
目录springboot打印post请求原始入参body体post接收不到body里的参数(对象参数)检查content-typespringboot打印post请求原始入参body体1、首先定义过滤...

springboot打印post请求原始入参body体

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、实现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();
    }
 
    /**
     * 打印请求参数
     *
     * @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();
    }
}

post接收不到body里的参数(对象参数)

检查注解

  • @responsebody
  • @requestbody

SpringBoot打印POST请求原始入参body体方式

检查实体

接收实体类,set、get方法是否正确

检查content-type

是否是application/json

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。