将请求和响应与每个请求的会话 ID 保存在文本文件中,REST API Spring Boot

Save request and response with session id for each request in a text file, REST API Spring Boot

需要帮助以下列方式保存 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?

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

Spring provides a mechanism for configuring user-defined interceptors to perform actions before and after web requests.

Among the Spring request interceptors, one of the noteworthy interfaces is HandlerInterceptor, which can be used to log the incoming request by implementing the following methods:

preHandle() – this method is executed before the actual controller service method afterCompletion() – this method is executed after the controller is ready to send the response Furthermore, Spring provides the default implementation of HandlerInterceptor interface in the form of HandlerInterceptorAdaptor class which can be extended by the user.

Let’s create our own interceptor – by extending HandlerInterceptorAdaptor as:

@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

我从 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();

        }
    }