[英]How to use enum constant in request mapping annotation?
This is as enum of constants.
public enum LoginRequestMappingConstants
{
LOGIN("/login"),
LOGOUT("/logout"),
ADMINISTRATION("/Administration");
private LoginRequestMappingConstants(String requestMapping) {
this.requestMapping = requestMapping;
}
private String requestMapping;
public String getRequestMapping() {
return requestMapping;
}
}
In request mapping annotation I wanted to use the enum of constant.
@RequestMapping(value = LoginRequestMappingConstants.LOGIN.getRequestMapping(), method = RequestMethod.GET)
仅在编译期间出现此错误。 注释属性RequestMapping.value的值必须是一个常量表达式。 此错误是什么意思? 为RequestMapping注释创建常量的正确方法是什么?
您不能使用枚举; 您只能使用String
。 将这些路径放置在与控制器分开的地方通常没有任何意义,但是如果需要,请使用普通常量:
public interface LoginRequestMappings {
String LOGIN = "/login";
String LOGOUT = "/logout";
}
@RequestMapping(LoginRequestMappings.LOGIN)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.