【问题标题】:Save request and response with session id for each request in a text file, REST API Spring Boot使用文本文件中的每个请求的会话 ID 保存请求和响应,REST API Spring Boot
【发布时间】:2018-06-03 13:48:04
【问题描述】:

需要帮助以以下方式保存 REST API 的请求和响应,

Session ID:
Request:
{
   {
   request header
   }
   {
   request body
   }
}
Response:
{
    {
     response header
   }
   {
   response body
   }
}

这不应依赖于日志记录级别或任何其他与日志记录相关的概念。 检查了许多类似的问题,但没有答案, 谁能帮帮我,谢谢。

Spring Boot - How to log all requests and responses with exceptions in single place?

【问题讨论】:

    标签: java spring spring-boot httprequest httpresponse


    【解决方案1】:

    您可以使用 HandlerInterceptorAdapter,并在文件中写入您需要的信息:

    Spring 提供了一种配置用户自定义拦截器的机制 在 Web 请求之前和之后执行操作。

    在 Spring 请求拦截器中,值得注意的一个 接口是HandlerInterceptor,可以用来记录 通过实现以下方法传入请求:

    preHandle() – 该方法在实际控制器之前执行 服务方法 afterCompletion() – 此方法在 控制器已准备好发送响应 此外,Spring 提供 HandlerInterceptor 接口的默认实现形式 HandlerInterceptorAdaptor类,可由用户扩展。

    让我们创建自己的拦截器——通过扩展 HandlerInterceptorAdaptor 为:

    @Component public class TaxiFareRequestInterceptor extends
    HandlerInterceptorAdapter {
    
    @Override
    public boolean preHandle(
      HttpServletRequest request, 
      HttpServletResponse response, 
      Object handler) {
        return true;
    }
    
    @Override
    public void afterCompletion(
      HttpServletRequest request, 
      HttpServletResponse response, 
      Object handler, 
      Exception ex) {
        //
    } }
    

    http://www.baeldung.com/spring-http-logging

    http://www.baeldung.com/spring-mvc-handlerinterceptor

    【讨论】:

    【解决方案2】:

    我从 Gist https://gist.github.com/int128/e47217bebdb4c402b2ffa7cc199307ba 找到了答案 记录请求和响应。根据我的要求进行了一些小的更改,即写入文件而不是使用 java 7 功能进行日志记录。

    Path path = Paths.get("home/midoriya/sample.txt");
    String strValue = "Whatever the values want to write in file";
    Path path = Paths.get(fileName);
    byte[] bytes = strValue.getBytes();
    Files.write(path, bytes);
    

    FileWriter fw = null;
    BufferedWriter writer = null;
    //  File logFile = null;
    
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            LocalDate localDate = LocalDate.now();
            File logFile = new File("/home/ramesh/logReqRes"+localDate.getDayOfMonth()+localDate.getMonth()+".txt");
            boolean flag = logFile.createNewFile();
            System.out.println("flag :" + flag);
            if( flag || logFile.length() >= (1024*1024*1024)) 
                fw = new FileWriter(logFile, false);
            else
                fw = new FileWriter(logFile, true);
    
            writer = new BufferedWriter(fw);
            if (isAsyncDispatch(request)) {
                filterChain.doFilter(request, response);
            } else {
                doFilterWrapped(wrapRequest(request), wrapResponse(response), filterChain);
            }
        } catch (IOException io) {
            io.printStackTrace();
        }
        catch (Exception ex) {
            ex.printStackTrace();
    
        }
        finally {
    
            try {
    
                if (writer != null)
                    writer.close();
    
                if (fw != null)
                    fw.close();
    
            } catch (IOException ex) {
    
                ex.printStackTrace();
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 2019-10-13
      • 1970-01-01
      • 2016-04-23
      • 2018-09-11
      • 2015-09-16
      • 1970-01-01
      • 2014-03-29
      • 2018-11-17
      相关资源
      最近更新 更多