Home > OS >  Spring Boot - Is it possible to disable an end-point
Spring Boot - Is it possible to disable an end-point

Time:05-27

Assuming I have a controller like:

public class MyController {

  public String endpoint1() {...}

  public String endpoint2() {...}
}

I want to disable endpoint1 for whatever reason in Spring. Simply, just disable it so that it cannot be accessed. So, I am not looking for how and what response to return in that case or how to secure that endpoint. Just looking to simply disable the endpoint, something like @Disabled annotation on it or so.

CodePudding user response:

Why not remove the mapping annotation over that method?

CodePudding user response:

Try this simple approach: You can define a property is.enable.enpoint1 to turn on/off your endpoint in a flexible way.

If you turn off the endpoint, then return a 404 or error page, which depends on your situation.

@Value("${is.enable.enpoint1}")
private String isEnableEnpoint1;
 
public String endpoint1() {
    if (!"true".equals(isEnableEnpoint1)) {
        return "404";  
    }
    // code
}
 

CodePudding user response:

You can use @ConditionalOnExpression annotation.

public class MyController {

  @ConditionalOnExpression("${my.controller.enabled:false}")
  public String endpoint1() {...}

  public String endpoint2() {...}
}

In application.properties, you indicates that controller is enabled by default

my.controller.enabled=true

ConditionalOnExpression sets false your property, and doesn't allow access to end-point

  • Related