如何在请求映射注释中使用枚举常量?

时间:2015-09-02 07:29:53

标签: java spring-mvc

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注释创建常量的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

你不能使用枚举;你只能使用String。将这些路径放在与控制器不同的位置通常没有意义,但如果确实需要,请使用普通常量:

public interface LoginRequestMappings {
    String LOGIN = "/login";
    String LOGOUT = "/logout";
}

@RequestMapping(LoginRequestMappings.LOGIN)
相关问题