http-inbound 网关是否可以做拦截器的 preHandle 和 postHandle 之类的事情?

Is http-inbound gateway can be do something like preHandle and postHandle of interceptor?

我想在服务接受 http 请求时将 "UUID" 放入 MDC。 因为方便日志搜索。

我继承了 HttpRequestHandlingMessagingGateway,发现 handleRequest() 是最终的,因此我无法覆盖它。

那么有没有办法在接受请求(MDC.put())和写入响应(MDC.remove())时做一些事情?

好吧,HttpRequestHandlingMessagingGateway 没有责任以这种方式操纵请求。

我认为您需要查看 Web 过滤器 注册:https://www.mkyong.com/spring-mvc/how-to-register-a-servlet-filter-in-spring-mvc/

作为一个想法,您可以借用现有的 AbstractRequestLoggingFilter:

 * Base class for {@code Filter}s that perform logging operations before and after a request
 * is processed.
 *
 * <p>Subclasses should override the {@code beforeRequest(HttpServletRequest, String)} and
 * {@code afterRequest(HttpServletRequest, String)} methods to perform the actual logging
 * around the request.