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

Java 如何在请求映射注释中使用枚举常量?,java,spring-mvc,Java,Spring Mvc,只有在编译时,我才得到这个错误。 注释属性RequestMapping.value的值必须是常量表达式。这个错误是什么意思? 为RequestMapping注释创建常量的正确方法是什么?您不能使用枚举;只能使用字符串。将这些路径与控制器分开放置通常没有意义,但如果确实需要,请使用普通常量: This is as enum of constants. public enum LoginRequestMappingConstants { LOGIN("/l

只有在编译时,我才得到这个错误。 注释属性RequestMapping.value的值必须是常量表达式。这个错误是什么意思?
为RequestMapping注释创建常量的正确方法是什么?

您不能使用枚举;只能使用
字符串
。将这些路径与控制器分开放置通常没有意义,但如果确实需要,请使用普通常量:

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)

不能使用枚举;只能使用
字符串
。将这些路径与控制器分开放置通常没有意义,但如果确实需要,请使用普通常量:

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)

每个注释参数值必须是编译时常量表达式。编译器并不认为对枚举调用getRequestMapping是常量表达式。例如,您可以使用普通字符串常量(
publicstaticfinalstringlogin\u MAPPING=“/LOGIN”
)。或者,Spring允许您在属性文件中定义映射,并像使用
@RequestMapping(“${mapping.login}”)
一样使用它,每个注释参数值必须是编译时常量表达式。编译器并不认为对枚举调用getRequestMapping是常量表达式。例如,您可以使用普通字符串常量(
publicstaticfinalstringlogin\u MAPPING=“/LOGIN”
)。或者,Spring允许您在属性文件中定义映射,并像使用
@RequestMapping(“${mapping.login}”)