找不到Spring控制器

时间:2017-01-08 00:27:18

标签: spring spring-web spring-webflux

我正在尝试使用spring web reactive创建一个RESTful服务。我有一个具有通常结构的控制器

package com.hcl.bc4sc.server.controller;
...
@RestController
@RequestMapping("/api/v1/registrar/enroll")
public class ControllerV1RegistrarEnroll {
...
    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
    public Mono<ResponseEntity> registrarEnrollPost(@RequestBody final UserInfo userInfo) {
...

我正在使用@ComponentScan来获取像这样注册的控制器

@Configuration
@ComponentScan(basePackages = "com.hcl.bc4sc.server.controller")
public class ServerInitialization {
...

正在定义我的ServerInitialization类中的各种bean,因此我知道Spring正在处理ServerInitialization。

我想知道问题是否与我启动Spring和HttpServer的方式一样:

public static void boot() throws TimeoutException {
    final GenericApplicationContext context
            = new AnnotationConfigApplicationContext(DelegatingWebReactiveConfiguration.class,
                                                     ServerInitialization.class);
    final HttpHandler handler = DispatcherHandler.toHttpHandler(context);
    final ServerConfig config = context.getBean(ServerConfig.class);
    applicationContext = Optional.of(context);

    // Reactor Netty
    final Function<HttpChannel, Mono<Void>> adapter
            = new ReactorHttpHandlerAdapter(handler);
    final HttpServer server = HttpServer.create(config.getHost(), config.getPort());
    httpServer = Optional.of(server);
    server.ws("", adapter).startAndAwait();
}

当我尝试测试时,我使用网址http://localhost/api/v1/registrar/enroll。它返回404。

如果我应该以不同的方式开始我的服务,有人可以指出一个完整的工作示例吗?

1 个答案:

答案 0 :(得分:0)

根据您提到的代码,您的控制器位于

com.hcl.bc4sc.server.config

但您正在进行的扫描是针对包裹:

com.hcl.bc4sc.server.controller

你应该首先解决这个歧义。并且知道它是否仍然失败。启动日志会有所帮助。

相关问题