Search code examples
javaspringurlrequest-mapping

Insert referenced variable into RequestMapping


Hi I'm forcing with problem connected to @RequestMapping. I'm trying to insert variable into URL from another class and make range in regular expression. Could you tell me if it's possible?

Controller:

@RequestMapping(value = "/v{:[1-currentVersion]}/dictionary")

Another Class With currentVersion value

public static int currentVersion = 1;

Solution

  • Unfortunately no, you can't do this in such way, because annotations doesn't support non-constant attribute values. Only if currentVersion can be final - it will work:

    public final static int currentVersion = 1;
    // ...
    @RequestMapping(value = "/v{:[1-"+currentVersion+"]}/dictionary")
    

    Furthermore, from general point of view, request mapping it self is quite static thing. If you have some dynamically changing values, you can only check them inside your method body.