Spring controllers not being found

191 views Asked by At

I am trying to create a RESTful service using spring web reactive. I have a controller that has the usual structure

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) {
...

I am using @ComponentScan to get the controller registered like this

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

The various beans in my ServerInitialization class are being defined, so I know that ServerInitialization is being processed by Spring.

I am wondering if the problem might be with the way I am starting Spring and the HttpServer like this:

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();
}

When I try to test this I use the url http://localhost/api/v1/registrar/enroll. It returns a 404.

If I should be starting my service differently, could somebody please point me at a good complete working example?

1

There are 1 answers

1
Himanshu Bhardwaj On

As per the code you mentioned in question, your controller is in

com.hcl.bc4sc.server.config

But the scan you are doing is for package:

com.hcl.bc4sc.server.controller

You should first fix this ambiguity. And let know of if it still fails. Startup Logs would help.