處理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);
    };
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM