Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过RouterFunctionMapping在Spring5WebFlux中注册多个RouterFunction?_Java_Spring_Kotlin - Fatal编程技术网

Java 如何通过RouterFunctionMapping在Spring5WebFlux中注册多个RouterFunction?

Java 如何通过RouterFunctionMapping在Spring5WebFlux中注册多个RouterFunction?,java,spring,kotlin,Java,Spring,Kotlin,根据webflux文档,可以使用RouterFunctionMapping扫描RouterFunction: 路由函数映射 — 检测Spring配置中的一个或多个RouterFunction bean,通过RouterFunction.andOther组合它们,并将请求路由到生成的组合RouterFunction 有没有注册处理程序的好例子?只要创建您想要的RouterFunction bean: @Configuration public class RoutesConfig { @B

根据webflux文档,可以使用RouterFunctionMapping扫描RouterFunction:

路由函数映射 — 检测Spring配置中的一个或多个RouterFunction bean,通过RouterFunction.andOther组合它们,并将请求路由到生成的组合RouterFunction


有没有注册处理程序的好例子?

只要创建您想要的RouterFunction bean:

@Configuration
public class RoutesConfig {
    @Bean
    public RouterFunction helloRoutesV1() {
        return RouterFunctions.route(RequestPredicates.path("/v1/hello-world"),
                request -> ok().body(fromObject("Hello World v1!")));
    }

    @Bean
    public RouterFunction helloRoutesV2() {
        return RouterFunctions.route(RequestPredicates.path("/v2/hello-world"),
                request -> ok().body(fromObject("Hello World v2!!!")));
    }
}

只需创建所需数量的RouterFunction bean:

@Configuration
public class RoutesConfig {
    @Bean
    public RouterFunction helloRoutesV1() {
        return RouterFunctions.route(RequestPredicates.path("/v1/hello-world"),
                request -> ok().body(fromObject("Hello World v1!")));
    }

    @Bean
    public RouterFunction helloRoutesV2() {
        return RouterFunctions.route(RequestPredicates.path("/v2/hello-world"),
                request -> ok().body(fromObject("Hello World v2!!!")));
    }
}

我也这么认为,但当定义了多个bean时,httpServer初始化失败,原因是:
config.WebConfiguration中方法httpServer的参数0需要一个bean,但找到了2个:-rootRouterFunction:由类路径资源[RouterConfiguration.class]中的方法“rootRouterFunction”定义-productRouterFunction:由类路径资源[RouterConfiguration.class]
中的方法“productRouterFunction”定义,也许config.WebConfiguration是您的配置,它具有httpServer(…)方法。正如您所看到的,Spring不知道向这个方法注入什么,所以您必须重写这个方法或使用单个RouterFunction bean。我构建并运行代码时没有任何错误。弹簧靴2.0.0.0版本。我使用了由生成的项目模板,并添加了routeConfig.java。运行和测试就足够了。谢谢。。。这有点奇怪。在WebConfiguration中,我只对httpServer进行了如下初始化:
@Bean fun httpServer(routerFunction:routerFunction):httpServer?{val httpHandler=RouterFunctions.toHttpHandler(routerFunction)val adapter=ReactorHttpHandlerAdapter(httpHandler)val server=HttpServer.create(environment.getProperty(SERVERHOST),Integer.valueOf(environment.getProperty(SERVERPORT)))server.newHandler(适配器)return server…
我想GA版本不再需要它了。当@EnableWebFlux注释被删除时,bean也被发现了,但当定义了多个bean时,httpServer初始化失败,原因是:
config.WebConfiguration中方法httpServer的参数0需要一个bean,但找到了2个:-rootRouterFunction:由类路径资源[RouterConfiguration.class]中的方法“rootRouterFunction”定义,-productRouterFunction:由类路径资源[RouterConfiguration.class]中的方法“productRouterFunction”定义。
也许config.WebConfiguration是您的配置,并且它具有httpServer(…)方法。正如您所看到的,Spring不知道向这个方法注入什么,所以您必须重写这个方法或使用单个RouterFunction bean。我构建并运行代码时没有任何错误。Spring Boot 2.0.0.RELEASE。我使用由生成的项目模板并添加了RouteConfig.java。运行和测试它就足够了。谢谢……这有点奇怪。在在WebConfiguration中,我只对httpServer进行了如下初始化:
…@Bean fun httpServer(routerFunction:routerFunction):httpServer?{val httpHandler=RouterFunctions.toHttpHandler(routerFunction)val adapter=ReactorHttpHandlerAdapter(httpHandler)val server=httpServer.create(environment.getProperty(SERVERHOST)、Integer.valueOf(environment.getProperty(SERVERPORT)))server.newHandler(适配器)返回服务器…
我想GA发行版不再需要它了。删除@EnableWebFlux注释后,发现了bean