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

安稳与你 提交于 2019-11-28 04:57:41

问题


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?


回答1:


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.




回答2:


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"



来源:https://stackoverflow.com/questions/14213723/the-value-for-annotation-attribute-requestmapping-value-must-be-a-constant-expre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!