【问题标题】:How to save all APIs in Spring Boot application to database?如何将 Spring Boot 应用程序中的所有 API 保存到数据库?
【发布时间】:2019-07-20 02:31:28
【问题描述】:

我必须在 Spring Boot 应用程序中保存所有 API 请求以及对数据库的响应。

我应该保存以下内容:

request URL
request parameters
request body
response body

我怎样才能通用地做到这一点,这样我就不需要在每个方法中编写代码? 请建议如何做到这一点

【问题讨论】:

标签: spring-boot spring-mvc netflix-zuul


【解决方案1】:

似乎有很多关于如何使用过滤器记录这些内容的示例,因此您可以将存储库注入其中并保存而不是记录。

编辑 - 抱歉,未能包含 chain.doFilter(request, response);

  //Filter
    @Component
    public class RequestResponseLoggingFilter implements Filter {

        private final Logger LOGGER = LoggerFactory.getLogger(Application.class);

        //Inject repo here

        @Override
        public void doFilter(
          ServletRequest request, 
          ServletResponse response, 
          FilterChain chain) throws IOException, ServletException {

            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse res = (HttpServletResponse) response;

            //Read data from req and res, save to repo
            LOGGER.info("Logged : " + req.getRequestURI());

            chain.doFilter(request, response);
        }

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {}

        @Override
        public void destroy() {}
    }
    @Bean
    public FilterRegistrationBean<RequestResponseLoggingFilter> loggingFilter(){
        FilterRegistrationBean<RequestResponseLoggingFilter> registrationBean 
          = new FilterRegistrationBean<>();

        registrationBean.setFilter(new RequestResponseLoggingFilter());
        registrationBean.addUrlPatterns("/**");

        return registrationBean;    
    }

Unable to autowire the service inside my authentication filter in Spring 是您可能需要的东西。

【讨论】:

猜你喜欢
  • 2020-02-12
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 2020-04-06
  • 1970-01-01
  • 2018-05-12
相关资源
最近更新 更多