The value of the RequestMapping.value annotation attribute must be a constant expression

Using the following code snippet:

public class MyUrls { // properties get initialized using static{...} public final static String URL_HOMEPAGE = properties.getProperty("app.homepage"); } @Controller public class HomepageController { @RequestMapping(MyUrls.URL_HOMEPAGE) public String homepage() { return "/homepage/index"; } } 

I get the following error:

 The value for annotation attribute RequestMapping.value must be a constant expression 

But in fact, URL_HOMEPAGE is a constant, as it is declared as public final static . Am I mistaken? How to solve this problem?

+4
source share
2 answers

As long as URL_HOMEPAGE is a constant, the value of which may not be, it can only be determined at run time. I believe that the values ​​used in annotations should be resolvable at compile time.

+7
source

This is a constant, but it is initialized after initializing the display of the request. You call properties.getProperty("app.homepage"); When the class loader loads your class, URL_HOMEPAGE is not yet initialized, hence an error.
You must specify an initialized string such as "/ path / subpath" as a parameter

+1
source

All Articles