Annotations for a function that displays REST endpoints

I have a spring controller with multiple endpoints (REST). I want to call several instances of this controller, where each instance will have several endpoints, selectively enabled / disabled.

Based on my reading so far, togglz provides feature switching, but it does not enable / disable REST endpoints (togglz provides an API so that caller code can check if the feature is enabled); ff4j seems like another alternative, but it wasn’t very obvious from the documentation if it could enable / disable REST endpoints

I read the Feature Toggling Java Annotations thread, but this is a longer implementation. Is there any package that I can use to specify the endpoints that need to be enabled / disabled in the configuration file, and use the annotation on the REST endpoints to disable / enable them (thus, the logic in my method remains untouched and minimizes testing )

+5
source share
1 answer

A class with @Bean or @Component will be loaded by spring when launched through the bean visitor mechanism. To exclude this bean from the spring context at startup, you can create a BeanPostProcessor ( here ) and check the highlighted annotation, BUT as far as I understand, you cannot return the bean to the context at runtime.

As a result, you must make this bean “smart” in order to perform the correct / mock operation (or send HTTP code 503) when requests are received.

FF4j can really help you implement this behavior, but not with one annotation on top of your REST controller. What could you do:

  • Create an interface, annotate the interface using the special annotation FF4J
  • Create 2 interface implementations, each time with a different name
  • Use FF4J to select an implementation or at run time.

Here is the code snippet to get the idea:

 public interface GreetingService { @Flip(name = "theFeatureIDToToggle", alterBean = "greeting.french") String sayHello(String name); } @Component("greeting.french") public class GreetingServiceFrenchImpl implements GreetingService { public String sayHello(String name) {return "Bonjour " + name; } @Component("greeting.english") public class GreetingServiceEnglishImpl implements GreetingService { public String sayHello(String name) {return "Hello " + name; } //... import @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-ff4j-aop-test.xml") public class FeatureAdvisorTest { @Autowired private FF4j ff4j; @Autowired @Qualifier("greeting.english") private GreetingService greeting @Test public void testAnnotatedFlipping_with_alterBean() { ff4j.disable("theFeatureIDToToggle"); Assert.assertTrue(greeting.sayHello("CLU").startsWith("Hello")); ff4j.enable("theFeatureIDToToggle"); Assert.assertTrue(greeting.sayHello("CLU").startsWith("Bonjour")); } } 

You can switch one method or the whole class, since you want all the samples to be available here .

+3
source

Source: https://habr.com/ru/post/1233666/


All Articles