Spring Boot
@RestController
Enable/Disable Controller
Application Properties
Java Annotations

Enable or disable a Spring Boot @RestController using properties

Codemia Knowledge Hub ·

Yes. Annotate the @RestController with @Conditional and a custom Condition that reads a property, then set that property in application.properties or application.yml. If you only need to switch controllers per environment, @Profile does the job without custom code.

How conditional beans work

Spring Boot uses conditional annotations to decide whether a bean is added to the application context: the bean is created only when its condition is met. @Conditional is often used on @Configuration classes and @Bean methods, but it can also be placed directly on a component class such as a @RestController, which is what the property-based approach below does.

Switching a controller by profile with @Profile

@Profile enables a bean only when one of the named profiles is active. Define profiles like dev, prod or test and attach the controller to one of them:

java
1@RestController
2@Profile("dev")
3public class MyDevController {
4    @GetMapping("/dev")
5    public String devEndpoint() {
6        return "This is the dev endpoint.";
7    }
8}

MyDevController is registered only when the application runs with the dev profile.

Disabling a controller with a property

Write a custom condition

Create a class that implements Condition from org.springframework.context.annotation:

java
1public class OnPropertyCondition implements Condition {
2
3    @Override
4    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
5        String isEnabled = context.getEnvironment().getProperty("custom.controller.enabled");
6        return isEnabled != null && isEnabled.equalsIgnoreCase("true");
7    }
8}

Apply the condition to the controller

java
1@RestController
2@Conditional(OnPropertyCondition.class)
3public class MyConditionalController {
4    
5    @GetMapping("/conditional")
6    public String conditionalEndpoint() {
7        return "This endpoint is conditionally active.";
8    }
9}

Set the property

In application.properties:

properties
# application.properties
custom.controller.enabled=true

Or in YAML:

yaml
1# application.yml
2custom:
3  controller:
4    enabled: true

Changing custom.controller.enabled turns MyConditionalController on or off.

Profiles or conditions

  • Default behavior: Controllers are enabled unless a condition disables them.
  • Profiles vs. conditions: Profiles suit environment-specific setups. Conditions give finer control within the same environment.
  • Complex conditions: A custom condition can check several properties or runtime state, but keep it no more complex than needed.
MethodUsagePros & Cons
@ProfileUse profiles for environment-specific controllers.Pros: Easy to configure. Cons: Limited to profiles.
@Conditional with propertiesUse property flags to enable/disable controllers.Pros: Fine-grained control. Cons: Requires custom code.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms