运行 Spring WebFlux @Controller 没有 Spring 引导

Run Spring WebFlux @Controller without Spring Boot

我试图在没有 Spring 引导的情况下从主程序启动 Spring 应用程序,我设法使路由器功能正常工作,但无法使经典的@Controllers 正常工作。 我调试并观察了启动源和内置配置,添加了 RequestMappingHandlerMapping,它注册了我的控制器,但无论如何 WebHandler 看不到它。 我可以看到 spring 发现我的 @Controller 作为一个 bean,所以这不是问题。 有人能帮我吗?谢谢

当我运行申请时,我得到:

Apr 13, 2018 7:27:30 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@20e2cbe0: startup date [Fri Apr 13 19:27:30 EEST 2018]; root of context hierarchy Apr 13, 2018 7:27:31 PM org.springframework.web.reactive.result.method.AbstractHandlerMethodMapping$MappingRegistry register INFO: Mapped "{[/test],methods=[GET]}" onto public java.lang.String test.wboot.Controller.test() Apr 13, 2018 7:27:31 PM org.springframework.web.reactive.result.method.AbstractHandlerMethodMapping$MappingRegistry register INFO: Mapped "{[/test],methods=[GET]}" onto public java.lang.String test.wboot.Controller.test() [DEBUG] (main) Using Console logging [DEBUG] (main) Default Epoll support : false [DEBUG] (main) Default KQueue support : false [DEBUG] (main) Connecting new channel: AbstractBootstrap$PendingRegistrationPromise@4a3329b9(incomplete) Press button to exit

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.start();
    }
}

.

@Configuration
@ComponentScan("test.wboot")
public class AppConfig {

    public static final int port = 9099;

    @Bean
    public NettyContext servak(ApplicationContext applicationContext) throws IOException {


//        this handler works just perfect
//        HttpHandler httpHandler = RouterFunctions.toHttpHandler(routingFunction());

//        And this one can not see Controller
        HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build();

        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
        NettyContext localhost = HttpServer.create("localhost", port).newHandler(adapter).block();
        System.out.println("Press button to exit");
        System.in.read();
        return localhost;
    }

    public RouterFunction<ServerResponse> routingFunction() {
        return nest(path("/test"), route(GET(""), new HandlerFunction<ServerResponse>() {
            public Mono<ServerResponse> handle(ServerRequest request) {
                return ServerResponse.ok().syncBody("Hello yopta");
            }
        }));
    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping(ApplicationContext applicationContext) {
        RequestMappingHandlerMapping requestMappingHandlerMapping = new RequestMappingHandlerMapping();
        requestMappingHandlerMapping.setApplicationContext(applicationContext);
        requestMappingHandlerMapping.afterPropertiesSet();
        return requestMappingHandlerMapping;
    }

    @Bean
    public WebHandler webHandler(ApplicationContext applicationContext) {
        DispatcherHandler webHandler = new DispatcherHandler();
        webHandler.setApplicationContext(applicationContext);
        return webHandler;
    }
}

.

@RestController
@RequestMapping("/test")
public class Controller {

    @GetMapping
    public String test() {
        return "work";
    }
}

Maven 依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>io.projectreactor.ipc</groupId>
        <artifactId>reactor-netty</artifactId>
        <version>0.7.6.RELEASE</version>
    </dependency>
</dependencies>

你为什么要重新定义一切? Spring 框架提供了一个 @EnableWebFlux 注释来执行此操作以及更多操作,因此您应该将该注释添加到 @Configuration class.

然后您只需 to follow the instructions here 使用框架自动创建的 WebHandler 启动您的服务器;所以对于 Reactor Netty,这看起来像:

HttpHandler handler = ...
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer.create(host, port).newHandler(adapter).block();