0

I am developing a spring-boot custom starter, and I want my components/beans to be initialized only if some conditions are met. These beans include a REST controller. I have made a @Configuration class for that purpose:

@Configuration
@ConditionalOnWebApplication
@Conditional(MyConditions.class)
public class MyConfiguration {

    @Bean
    public HandlerMapping registerController() {
        SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();

        Map<String, Object> urlMap = new HashMap<>();

        urlMap.put("/custom", myController());

        handlerMapping.setUrlMap(urlMap);

        return handlerMapping;
    }

    @Bean
    public MyController myController() {
        return new MyController();
    }

MyController class is:

public class MyController {

    @GetMapping
    public String hello() {
        return "I'm here";
    }
}

What I am trying to achieve is that localhost:8080/custom answers I'm here, but instead of that what I get is:

No adapter for handler [MyController@355b46cb]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler","path":"/custom"

I don't want any of my beans to be initialized if conditions are not met, that is why MyController is not annotated with @Controller or @RestController.

Any ideas on how I can achieve this?

Note: to test this, I have created a minimalistic SpringBoot project with a controller and I have added my custom starter as a dependency. MyConfiguration conditions are matched and beans are registered.

dcordonu
  • 43
  • 7

0 Answers0