如何让Spring Integration的Http.inboundGateway忽略指定路径?
解决Spring Integration Http.inboundGateway忽略Swagger UI路径的问题
方法一:调整Inbound Gateway的路径排除规则
Spring Integration的Http.inboundGateway支持Ant路径的排除语法,直接在路径配置里添加需要忽略的路径即可,注意要同时覆盖Swagger UI的静态资源路径:
@Bean public HttpRequestHandlingMessagingGateway httpInboundGateway() { HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(); // 匹配所有路径,但排除Swagger UI相关路径 gateway.setPathPatterns("/**", "!/swagger-ui.html", "!/swagger-ui/**"); // 其他原有配置(请求映射、消息通道等) return gateway; }
这里的!/xxx表示排除该路径,只排除单个/swagger-ui.html还不够,因为Swagger UI会加载/swagger-ui/**下的CSS、JS等静态资源,必须一并排除才能正常访问UI页面。
方法二:调整请求处理优先级
通过设置Inbound Gateway的order属性降低其优先级,让Spring MVC的DispatcherServlet先处理Swagger相关请求:
@Bean public HttpRequestHandlingMessagingGateway httpInboundGateway() { HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(); gateway.setPathPatterns("/**"); // 数值越大优先级越低,确保Swagger的请求先被处理 gateway.setOrder(100); // 其他原有配置 return gateway; }
方法三:XML配置方式(若使用XML定义网关)
如果是用XML配置Inbound Gateway,直接通过excluded-paths属性指定要忽略的路径:
<int-http:inbound-gateway id="proxyGateway" request-channel="proxyRequestChannel" path="/**" excluded-paths="/swagger-ui.html,/swagger-ui/**"/>
验证
配置完成后重启应用,访问/swagger-ui.html即可正常加载界面,其他路径仍会正常代理到第三方服务。
内容的提问来源于stack exchange,提问作者Marc Tarin
相关产品推荐
相关产品推荐

