Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When 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 does be a constant, since it is declared as public final static. Am I wrong? How to solve this issue?

like image 501
sp00m Avatar asked Jan 08 '13 11:01

sp00m


2 Answers

Whilst URL_HOMEPAGE is a constant it's value may not be, it can only be determined at runtime. I believe that values used in annotations must be resolvable at compile-time.

like image 75
Jonathan Avatar answered Oct 05 '22 23:10

Jonathan


It is a constant, but it is initialized after the request mapping is initialized. You are calling properties.getProperty("app.homepage"); When the classloader loads you class, the URL_HOMEPAGE is not initialized yet, hence the error.
You need to give as a parameter an initialized string, such as "/path/subpath"

like image 33
Amir Kost Avatar answered Oct 06 '22 01:10

Amir Kost