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:
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:
Apply the condition to the controller
Set the property
In application.properties:
Or in YAML:
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.
| Method | Usage | Pros & Cons |
@Profile | Use profiles for environment-specific controllers. | Pros: Easy to configure. Cons: Limited to profiles. |
@Conditional with properties | Use property flags to enable/disable controllers. | Pros: Fine-grained control. Cons: Requires custom code. |
Related reading
- Can an abstract class have a constructor?
- Can an int be null in Java?
- Can an interface extend multiple interfaces in Java?
- Can constructors throw exceptions in Java?
- Can enums be subclassed to add new elements?
- Can I add jars to Maven 2 build classpath without installing them?
- Can I catch multiple Java exceptions in the same catch clause?
- Can I define System Properties within Spring Boot configuration files?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.