首頁 > Java > java教程 > 主體

springBoot之怎麼取得介面請求資料和回傳資料實作日誌

WBOY
發布: 2023-05-13 08:25:12
轉載
1578 人瀏覽過

    一、取得介面請求的資料

    可以在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 中
        }
     
        /**
         * 重写父类的 getWriter() 方法,将响应数据缓存在 PrintWriter 中
         */
        @Override
        public PrintWriter getWriter() throws IOException {
            try{
                pwrite = new PrintWriter(new OutputStreamWriter(bytes, "utf-8"));
            } catch(UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return pwrite;
        }
     
        /**
         * 获取缓存在 PrintWriter 中的响应数据
         * @return
         */
        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
                
            }
     
        }
     
    }
    登入後複製

    以上是springBoot之怎麼取得介面請求資料和回傳資料實作日誌的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    相關標籤:
    來源:yisu.com
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!