AroundAround
Sign in Sign up
Menu
  • Home
    • Home
  • Blog
    Technology
    All Technology Software Engineering Web Applications Java Spring Framework Spring Boot AWS
    All Posts
    Smash Posts Transportation Posts Driving Posts Multi Location Post
    Transport
    Driving Articles Driving License Resources Refresh Drive Success Stories Special Articles
    Research
    Popular lists Weblogs Tutorials
    News
    Education Health Sports
    Traffic Signs
    Dubai Traffic Signs Qatar Traffic Signs Abu Dhabi Traffic Signs Saudi Arabia Traffic Signs Kuwait Traffic Signs Kerala Traffic Signs
  • Insights
    Interview Series
    All Interview Series Java Questions Spring Questions Microservices Questions Database Questions
    Lifestyle
    Umrah Articles Dubai UAE visa 180 days calculator Qatar residence visa 180 days calculator Prayer Time Malappuram Sqm to Cent calculator Kerala
    City Explorer
    Sulthan Bathery
  • Mock Tests
    Driving
    Test Home
    Dubai
    RTA Theory Test Parking Knowledge Test Deep-dive Assessment Test
    Abu Dhabi
    RTA Driving Theory Test Deep-dive Assessment Test Saudi Arabia Computer Test
    Other Regions
    Qatar Driving Theory Test Kuwait Driving Theory Test Ajman RTA Theory Test Sharjah RTA Theory Test Bahrain Driving Theory Test Learners License Test - Kerala
    Education
    Kindergarten School
  • Course & Videos
    • CoursesEnroll today
    • Videos20+Watch & Subscribe
  • Questions and Tags
    Topics
    Don't miss Million dollar questions Million Views Day to day life Interview Junky Trendy questions Theory wizard
    Technology
    Java Spring AWS
    Find the differences
    All Find the differences List
    Tags
    macOS AWS Spring
  • About Us
    • Privacy Policy
    • Contact
    • Terms & Conditions
    • Cancellation & Refund Policy
    • Shipping & Delivery Policy
  1. Home
  2. spring-boot
  3. Post

Toggle Spring Boot RestController via Properties

Emma Brown
admin
#spring-boot #controller
Share post:
Share

Spring Boot REST controller: Conditional endpoint startup? Avoid runtime checks; discover the elegant annotation-based solution!

Can Spring Boot conditionally disable a `@RestController` (or its methods) based on a property's presence/absence, without resorting to in-method checks? Profiles are not a suitable solution.

Solution in a Nutshell

Enable/Disable Spring Boot @RestController via Properties

Yes, you can enable/disable a Spring Boot @RestController using properties. Avoid directly manipulating annotations; instead, use conditional logic within your controller or a surrounding configuration.

Method 1: Conditional on Property

@Component
public class MyRestController {

    @Autowired
    private Environment environment;

    @GetMapping("/api/data")
    public String getData() {
        if (environment.getProperty("my.rest.controller.enabled", Boolean.class, false)) {
            return "Data";
        } else {
            return "Controller disabled";
        }
    }
}

Set my.rest.controller.enabled=true (or false) in your application.properties or application.yml.

Method 2: Conditional on Profile

@Profile("!disabled") // active unless profile 'disabled' is set
@RestController
public class MyRestController {
    // ...Controller Methods
}

Activate the profile using --spring.profiles.active=myprofile (replace myprofile as needed) during application startup.

Method 3: Custom Bean Definition

@Configuration
public class RestControllerConfig {
    @Bean
    @ConditionalOnProperty(value = "my.rest.controller.enabled", havingValue = "true")
    public MyRestController myRestController(){
        return new MyRestController();
    }
}

This creates the bean only if the property is true.

Choose the method best suited to your application's complexity. Method 2 offers cleaner profile management; Method 3 provides more fine-grained control; Method 1 offers simple conditional logic. Remember to handle potential exceptions appropriately in production code.

Sidebar

Search

Tags

#spring-boot #controller

Trending posts

Post

HikariCP Shutdown Issue in Spring Boot

Post

Spring Boot REST Client

Post

Date Range Check with Spring Data JPA

Post

Spring Boot Spring Version Upgrade

  • Contact Us
  • Privacy Policy

© Copyright - smashplus 2013-25.