博主信息
博文 2
粉丝 0
评论 0
访问量 5299
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
SpringBoot打印POST请求原始入参body体方式
场子里的小哥哥
原创
1589人浏览过

这篇文章主要介绍了SpringBoot打印POST请求原始入参body体方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
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 {

@Beanpublic 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 = “, “;

@Overridepublic int getOrder() {    return order;}@Overrideprotected 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方法是否正确

检查Content-Type
是否是application/json


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学