ServletResponse获取响应体 日志记录出参

Posted chenhao0302

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ServletResponse获取响应体 日志记录出参相关的知识,希望对你有一定的参考价值。

有个需要日志记录http请求和响应的需求,通过一个logFilter来实现

 

一、入参打印

 JSON.toJSONString(servletRequest.getParameterMap()));

  

二、出参打印

ServletResponse中将输出流,直接获取流并不能获取流中的数据,只可以改写响应流,替换响应内容;
所以需要重写ServletResponse来保存传到输出流的内容;

com.github.isrsal.logging.ResponseWrapper 则实现了这个需求,将输出流的内容保存在了字节数组缓冲区;

需要引入的依赖:
<dependency>
            <groupId>com.github.isrsal</groupId>
            <artifactId>spring-mvc-logger</artifactId>
            <version>0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
</dependency>

 

获取出参的方法:

ResponseWrapper responseWrapper = new ResponseWrapper(Thread.currentThread().getId(), (HttpServletResponse) servletResponse);
        filterChain.doFilter(servletRequest, responseWrapper);
String str = new String(responseWrapper.toByteArray(), responseWrapper.getCharacterEncoding());

  

三、完整的出入参打印

 

// https://www.cnblogs.com/chenhao0302/p/9186346.html

import com.alibaba.fastjson.JSON; import com.github.isrsal.logging.ResponseWrapper; import org.apache.catalina.connector.ResponseFacade; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.logging.log4j.ThreadContext; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * @Author: 陈浩 * @Date: 2018-06-14 */ @Component @WebFilter(filterName = "logFilter", urlPatterns = "/*") public class LogFilter implements Filter { private static final Log log = LogFactory.getLog(LogFilter.class); @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { ThreadContext.put("TId", UUID.randomUUID().toString()); ResponseWrapper responseWrapper = new ResponseWrapper(Thread.currentThread().getId(), (HttpServletResponse) servletResponse); filterChain.doFilter(servletRequest, responseWrapper); Map params; // 记录入参 params = servletRequest.getParameterMap(); if (null != params) { log.info("入参:" + JSON.toJSONString(params)); } // 记录出参 String outParam = new String(); // 记录出参响应头 params = new HashMap(); // 如果响应头存在respCode则打印 try { params.put("respCode", ((ResponseFacade) servletResponse).getHeader("respCode")); params.put("respMsg", (URLDecoder.decode(((ResponseFacade) servletResponse).getHeader("respMsg"), "UTF-8"))); outParam = JSON.toJSONString(params); } catch (Exception e) { } // 记录出参响应体 outParam = outParam + new String(responseWrapper.toByteArray(), responseWrapper.getCharacterEncoding()); log.info("出参:" + outParam); ThreadContext.clearAll(); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }

  








以上是关于ServletResponse获取响应体 日志记录出参的主要内容,如果未能解决你的问题,请参考以下文章

HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码

HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码

java面试总结2

response 知识点

nestjs 中间件获取请求/响应体

Servlet总结