文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

springBoot之如何获取接口请求数据和返回数据实现日志

2023-05-14 14:08

关注

一、获取接口请求的数据

可以在Interceptor的afterCompletion中实现但是要重写RequestWrapper

代码记录如下:

HttpServletRequestFilter

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
 
@Component
@WebFilter(filterName = "HttpServletRequestFilter", urlPatterns = "/")
@Order(10000)
public class HttpServletRequestFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
 
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        if(servletRequest instanceof HttpServletRequest) {
            requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
        }
        //获取请求中的流如何,将取出来的字符串,再次转换成流,然后把它放入到新request对象中
        // 在chain.doFiler方法中传递新的request对象
        if(null == requestWrapper) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(requestWrapper, servletResponse);
        }
    }
 
    @Override
    public void destroy() {
 
    }
 
}

RequestWrapper

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*; 
 
public class RequestWrapper extends HttpServletRequestWrapper {
    private final String body;
 
    public RequestWrapper(HttpServletRequest request) {
        super(request);
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {
 
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        body = stringBuilder.toString();
    }
 
    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
        ServletInputStream servletInputStream = new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }
            @Override
            public boolean isReady() {
                return false;
            }
            @Override
            public void setReadListener(ReadListener readListener) {
            }
            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
        return servletInputStream;
 
    }
 
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }
 
    public String getBody() {
        return this.body;
    }
 
}

afterCompletion

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)
            throws Exception {
 
        logger.debug("SessionInterceptor");
        // 获取地址
        String url = request.getRequestURL().toString();
        String requestMethod = request.getMethod();
        String servletPath = request.getServletPath();
        String body = new RequestWrapper(request).getBody();
        String contentType = request.getContentType();
        Map reqMap = new HashMap();
        if(requestMethod.equals("POST")) {
            if(!contentType.equals("text/plain")) 
                body = "body is file,don't show.";
            if(body.length()>1000) 
                body = body.substring(0, 1000);
        }
        if(requestMethod.equals("GET")) {
            // 获取请求参数
            Map ParameterMap =  request.getParameterMap();
            Set<Map.Entry<String,String[]>> entry = ParameterMap.entrySet();
            Iterator<Map.Entry<String,String[]>> it = entry.iterator();
            while (it.hasNext()){
                Map.Entry<String,String[]>  me = it.next();
                String key = me.getKey();
                String value = me.getValue()[0];
                reqMap.put(key,value);
            }
        }
        logger.error("url: "+url+",requestMethod: "+requestMethod+",servletPath: "+servletPath+",body: " + body+",parameterMap: "+reqMap.toString());
        
        
    }

二、获取接口返回的数据

可以在filter中实现但是要重写ResponseWrapper,

代码记录如下:

HttpServletResponseFilter

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.Logger;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
 
@Component
@WebFilter(filterName = "HttpServletResponseFilter", urlPatterns = "/")
@Order(10000)
public class HttpServletResponseFilter implements Filter {
    static Logger logger = Logger.getLogger(HttpServletResponseFilter.class.getName());
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
 
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        ResponseWrapper mResp = new ResponseWrapper(resp); // 包装响应对象 resp 并缓存响应数据
        filterChain.doFilter(req, mResp);
        byte[] bytes = mResp.getBytes(); // 获取缓存的响应数据
        logger.error(new String(bytes,"utf-8"));
    }
 
    @Override
    public void destroy() {
 
    }
 
}

ResponseWrapper

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;
 
public class ResponseWrapper extends HttpServletResponseWrapper {
 
    private ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    private HttpServletResponse response;
    private PrintWriter pwrite;
 
    public ResponseWrapper(HttpServletResponse response) {
        super(response);
        this.response = response;
    }
 
    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return new MyServletOutputStream(bytes); // 将数据写到 byte 中
    }
 
    
    @Override
    public PrintWriter getWriter() throws IOException {
        try{
            pwrite = new PrintWriter(new OutputStreamWriter(bytes, "utf-8"));
        } catch(UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return pwrite;
    }
 
    
    public byte[] getBytes() {
        if(null != pwrite) {
            pwrite.close();
            return bytes.toByteArray();
        }
 
        if(null != bytes) {
            try {
                bytes.flush();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        return bytes.toByteArray();
    }
 
    class MyServletOutputStream extends ServletOutputStream {
        private ByteArrayOutputStream ostream ;
 
        public MyServletOutputStream(ByteArrayOutputStream ostream) {
            this.ostream = ostream;
        }
 
        @Override
        public void write(int b) throws IOException {
            ostream.write(b); // 将数据写到 stream 中
        }
 
        @Override
        public boolean isReady() {
            // TODO Auto-generated method stub
            return false;
        }
 
        @Override
        public void setWriteListener(WriteListener listener) {
            // TODO Auto-generated method stub
            
        }
 
    }
 
}

总结

本文参考了许多网上前辈们的踩坑历程,在此就不列举了,只做一记录

希望能给大家一个参考,也希望大家多多支持编程网。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯