Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically disable specific API in spring?

I have a flag DISABLE_FLAG and I want to use it to control multiple specific APIs in different controllers.

@RestController
public final class Controller1 {
    @RequestMapping(value = "/foo1", method = RequestMethod.POST)
    public String foo1()
}
@RestController
public final class Controller2 {
    @RequestMapping(value = "/foo2", method = RequestMethod.POST)
    public String foo2()
}

I can use an interceptor to handle all the urls. Is there a easy way to do that like annotation?

like image 955
eriee Avatar asked Aug 12 '26 10:08

eriee


2 Answers

You could use AOP to do something like that.

Create your own annotation...

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Maybe { }

and corresponding aspect...

@Aspect
public class MaybeAspect {

  @Pointcut("@annotation(com.example.Maybe)")
  public void callMeMaybe() {}

  @Around("callMeMaybe()")
  public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    // do your logic here..
    if(DISABLE_FOO) {
      // do nothing ? throw exception?
      // return null;
      throw new IllegalStateException();
    } else {
      // process the request normally
      return joinPoint.proceed();
    }
  }
}
like image 145
Izzy Avatar answered Aug 15 '26 05:08

Izzy


I don't think there is direct way to disable a constructed request mapping but We can disable API in many ways with some condition.

Here is the 2 ways disabling by spring profile or JVM properties.

public class SampleController {
    @Autowired
    Environment env;

    @RequestMapping(value = "/foo", method = RequestMethod.POST)
    public String foo(HttpServletResponse response) {
        // Using profile
        if (env.acceptsProfiles("staging")) {
            response.setStatus(404);
            return "";
        }

        // Using JVM options
        if("true".equals(System.getProperty("DISABLE_FOO"))) {
            response.setStatus(404);
            return "";
        }

        return "";
    }
}

If you are thinking futuristic solution using cloud config is the best approach. https://spring.io/guides/gs/centralized-configuration/

Using Conditional components

This allows to build bean with conditions, if the condition failed on startup, the entire component will never be built. Group all your optional request mapping to new controller and add conditional annotation

@Conditional(ConditionalController.class)
public class SampleController {
    @Autowired
    Environment env;

    @RequestMapping(value = "/foo", method = RequestMethod.POST)
    public String foo(HttpServletResponse response) {
        return "";
    }

    public static class ConditionalController implements Condition {

        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            return context.getEnvironment().acceptsProfiles("staging"); // Or whatever condition
        }

    }
}
like image 37
Pasupathi Rajamanickam Avatar answered Aug 15 '26 07:08

Pasupathi Rajamanickam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!