SpringBoot - Register Bean as Rest Controller

2.3k views Asked by At

I'm using Spring Boot AutoConfiguration for registering the beans. Need to find a way through which I can register the beans configured via Auto-Configuration as a Rest Controller

SampleController.java

public class SampleController
{
    @GetMapping("/sample-path")
    public String sampleMethod()
    {
        return "Sample String";
    }
}

SampleControllerAutoConfiguration.java

@Configuration
@ConditionalOnProperty(value = "some.property", havingValue = "true") // using this property, the parent app may or may not chose to have the Controller Endpoint
public class SampleControllerAutoConfiguration
{
    // Need to register this bean as a controller
    @Bean
    @ConditionalOnMissingBean
    public SampleController sampleController()
    {
        return new SampleController();
    }
}

I can't annotate SampleController with @RestController since it's in the same package as the parent project which imports this and hence get's auto-configured due to Component-Scan

0

There are 0 answers