处理webflux 项目 增加 content-path

需求: 增加路由前缀
项目: 基于webflux 的 r2dbc 建立的 mqtt项目

解决方案: 一

# springboot 项目
# 版本 >=2.3.release
spring:
  webflux:
    base-path: "/project-name"

解决方案: 二

server:
  servlet:
    context-path: "/project-name"
@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);
    };
}
posted @ 2021-05-08 14:39  空明师兄  阅读(1852)  评论(0编辑  收藏  举报