springBoot打印请求信息日志,如请求头,请求体,请求路径等

背景

和前端联调,前端总是说接口对了呀。
后端说,没有进我的方法呀。
后端加日志拦截所以请求。

过程

springmvc代码

包装类中报错getReader() has already been called for this request

springBoot打印请求信息日志,如请求头,请求体,请求路径等_第1张图片

  • 代码里面有这个包装类呀。
  • 这说明springBoot有自带的过滤器,同时也会调用这个方法。
  • 把优先级设置为最高。

依旧报getReader() has already been called for this request

  • 这次是在chain.doFilter(requestWrapper, response);里面报错
  • 估计有人用了原生的ServletRequest 了。
  • 加入
    @Override
    public ServletRequest getRequest() {
        return this;
    }

java.lang.StackOverflowError: null at org.springframework.web.util.WebUtils.getNativeRequest(WebUtils.java:463) ~[spring-web-5.1.5.RELEASE.jar:5.1.5.RELEASE]

  • 死循环,果然spring

后端报500,控制台日志不输出。

把日志过滤去掉用getRequest()方法。

代码


import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * Copyright (C), 2019-2019, 北京力盟亚有限公司
 * @Description: 过滤器
 * @Author: qianxi  
 * @Date: 2019/10/28 17:47
 * @version:1.0
 */
//@WebFilter(value="/*",filterName = "aaaaaLoggingFilter" )
@Configuration
public class LoggingFilterConfig implements Filter {
    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean filter = new FilterRegistrationBean(new LoggingFilterConfig());
        filter.addUrlPatterns("/*");
        //多个过滤器时执行顺序
        // 最高级别。
        filter.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        return filter;
    }

    private final static Log log = LogFactory.get();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest req = (HttpServletRequest)request;
        Map map = new HashMap();

        // Get request URL.
        map.put("URL", req.getRequestURL());
        map.put("Method", req.getMethod());
        map.put("Protocol",req.getProtocol());
        // 获取header信息

        List> headerList = new ArrayList<>();
        Map headerMaps = new HashMap();
        for(Enumeration enu = req.getHeaderNames(); enu.hasMoreElements();){
            String name = enu.nextElement();
            headerMaps.put(name,req.getHeader(name));
        }
        headerList.add(headerMaps);
        map.put("headers", headerList);
        //获取parameters信息

        List> parameterList = new ArrayList<>();
        Map parameterMaps = new HashMap();
        for(Enumeration names = req.getParameterNames();names.hasMoreElements();){
            String name = names.nextElement();
            parameterMaps.put(name, req.getParameter(name));
        }
        parameterList.add(parameterMaps);
        map.put("parameters", parameterList);
        String line = "";
        // 获取请求体信息
//            if (req.getMethod().equalsIgnoreCase("POST")) {
//                int len = req.getContentLength();
//                char[] buf = new char[len];
//                int bufcount = requestWrapper.getReader().read(buf);
//                if (len > 0 && bufcount <= len) {
//                    line = String.copyValueOf(buf).substring(0, bufcount);
//                }
//            } else if (req.getMethod().equalsIgnoreCase("GET")) {
        int idx = req.getRequestURL().indexOf("?");
        if (idx != -1) {
            line = req.getRequestURL().substring(idx + 1);
        } else {
            line = null;
        }
//            }

        if (line != null) {
            map.put("Context", new String[] { line });
        }
        log.info("接收请求报文:\n"+ JSONObject.toJSONString(map));
        chain.doFilter(request, response);
        // 辞书
        log.info("接收response报文:\n"+ response.getContentType());
    }

    @Override
    public void destroy() {

    }

}

参考资料

  • getReader() has already been called for this request
  • springMVC打印请求信息日志,如请求头,请求体,请求路径等
  • springboot中配置过滤器以及可能出现的问题

你可能感兴趣的:(#,力盟亚任职,#,spring家族,架构)