使用WebFlux的上下文路径 [英] Context Path with Webflux
本文介绍了使用WebFlux的上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试找到一种方法来设置WebFlux应用程序的上下文路径。我知道我可以使用
配置它server.servlet.context-path
如果我部署一个Servlet,但我希望使用WebFlux来实现它,而不必显式地将路径添加到每条路由或使用MVC。
推荐答案
您可以使用Web筛选器使WebFlux支持上下文路径
@Bean
public WebFilter contextPathWebFilter() {
String contextPath = serverProperties.getServlet().getContextPath();
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
if (request.getURI().getPath().startsWith(contextPath)) {
return chain.filter(
exchange.mutate()
.request(request.mutate().contextPath(contextPath).build())
.build());
}
return chain.filter(exchange);
};
}
这篇关于使用WebFlux的上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文