Spring controller Path not being considered a constant

I'd like to define a Spring's controller path with a path variable as:

private static final String IDS_REGEX = "[" + EnumSet.allOf(MyIdsEnum.class).stream().map(MyIdsEnum.class::getValue)
  .collect(Collectors.joining("|")) + "]";
public static final String MY_PATH = "/path/{id:" + IDS_REGEX + "]}";

And then in my controller:

 @PostMapping(value = MY_PATH, produces = MediaType.APPLICATION_JSON_VALUE)

However, IntelliJ throws an error in the PostMapping value:

Attribute value must be constant

The path is already static final, what should I have to do to turn it into a constant?

Thanks in advance

1 answer

  • answered 2019-06-25 09:04 Hypnotise

    It must be a compile time constant.

    A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

    • Literals of primitive type and literals of type String
    • Casts to primitive types and casts to type String
    • [...] operators [...]
    • Parenthesized expressions whose contained expression is a constant expression.
    • Simple names that refer to constant variables.
    • Qualified names of the form TypeName . Identifier that refer to constant variables.