@RequestMapping that is only mapped for @Profile
Asked Answered
C

3

6

Is it possible to create a method level @RequestMapping that is only mapped if a certain profile is active?

I know it is possible to have the controller created only if a specific profile is active, but I am referring specifically to method @RequestMapping s at the method level

Chug answered 2/10, 2017 at 20:48 Comment(0)
C
4

No. Profiles affect only bean creation, not method. So you either create the whole controller or no.

Your options:

1) Create a controller with the methods that have to be available only for the given profile.

2) If you don't want to create a dedicated controller for the given method that has to be created only for a given profile you can programmatically check the active profiles and return 404 or whatever you want.

@Autowired
Environment environment;

public boolean isMyProfileActive() {
    for (final String profileName : environment.getActiveProfiles()) {
        if("mySpecificProfile".equals(profileName)) return true;
    }   
    return false;
}

@RequestMapping(...)
public ResponseEntity<?> myMethod(){
     if(isMyProfileActive()) return new ResponseEntity(HttpStatus.NOT_FOUND);
     //the rest of the code for the method
}
Csch answered 2/10, 2017 at 21:1 Comment(1)
This makes me wonder if the class that parses @ RequestMapping can be overridden to implement this logic. Ex if I create another annotation like @ RequestMappingProfile("profile.name"). Then I can override the class that parses the @ RequestMapping annotations to also parse the @RequestMappingProfile annotation as well and only map the URI if the profile "profile.name" is enabled. I'm not sure what class parses the @ RequestMapping annotations though.Chug
S
2

No.

Taken from spring docs:

The @Profile annotation may be used in any of the following ways:

  • as a type-level annotation on any class directly or indirectly annotated with @Component, including @Configuration classes
  • as a meta-annotation, for the purpose of composing custom stereotype annotations
  • as a method-level annotation on any @Bean method

If a @Configuration class is marked with @Profile, all of the @Bean methods and @Import annotations associated with that class will be bypassed unless one or more of the specified profiles are active. This is very similar to the behavior in Spring XML: if the profile attribute of the beans element is supplied e.g., , the beans element will not be parsed unless profiles 'p1' and/or 'p2' have been activated. Likewise, if a @Component or @Configuration class is marked with @Profile({"p1", "p2"}), that class will not be registered/processed unless profiles 'p1' and/or 'p2' have been activated.

Salinger answered 2/10, 2017 at 20:59 Comment(0)
Z
0

But you can have multiple controllers, so put the profile specific methods into a new one and mark it with your @Profile("special_profile")

Zipporah answered 30/1, 2022 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.