我必须将所有API请求以及对数据库的响应保存在Spring Boot应用程序中。
我应该保存以下内容:
request URL
request parameters
request body
response body
如何通用地做到这一点,这样我就不需要在每个方法中都编写代码了? 请建议如何做
答案 0 :(得分:0)
关于如何使用过滤器记录那些记录,似乎有很多示例,因此您可以将存储库注入其中并保存而不是记录。
编辑-很抱歉,未能包含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是您可能需要的东西。