简体   繁体   English

Spring Boot 的 netty 上下文路径

[英]netty context path for spring boot

I am using spring boot with webflux and removed embedded tomcat dependency from starter web , I wanted to add base context path for my application , is there any way i can do ??我正在使用带有 webflux 的 spring boot 并从 starter web 中删除了嵌入式 tomcat 依赖项,我想为我的应用程序添加基本上下文路径,有什么办法吗? I need this because i have ingrees properties behind kubernetes cluster and redirection is made based on context path.我需要这个,因为我在 kubernetes 集群后面有 ingrees 属性,并且重定向是基于上下文路径进行的。

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
    </exclusions>

You cannot use spring web and spring webflux dependencies at the same time.您不能同时使用 spring web 和 spring webflux 依赖项。 If you do, spring will prioritize spring web and webflux filters will not be loaded at startup.如果你这样做,spring 将优先考虑 spring web 并且 webflux 过滤器不会在启动时加载。

During startup spring tries to create the correct ApplicationContext for you.在启动期间,spring 会尝试为您创建正确的 ApplicationContext。 As written here Spring boot Web Environment if Spring MVC (web) is on the classpath, it will prioritize this context.如此处所写Spring boot Web Environment如果 Spring MVC (web) 在类路径上,它将优先考虑此上下文。

A spring boot application is either a traditional web application, OR it's a webflux application. Spring Boot 应用程序要么是传统的 Web 应用程序,要么是 webflux 应用程序。 It cannot be both.不能两者兼而有之。

ContextPath is not something that is used in reactive programming so you have to filter each request and rewrite the path on each request. ContextPath 不是反应式编程中使用的东西,因此您必须过滤每个请求并重写每个请求的路径。

This should work, its a component webfilter that intercepts every request and then adds the context path that you define in application.properties这应该可以工作,它是一个拦截每个请求的组件 webfilter,然后添加您在application.properties定义的上下文路径

@Component
public class ContextFilter implements WebFilter {

    private ServerProperties serverProperties;

    @Autowired
    public ContextFilter(ServerProperties serverProperties) {
        this.serverProperties = serverProperties;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        final String contextPath = serverProperties.getServlet().getContextPath();
        final 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);
    }
}

But this will only work if your application is loaded as a Spring reactive application.但这只有在您的应用程序作为 Spring 响应式应用程序加载时才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM