请求映射和静态字段

Request mapping and static fields

我正在尝试为 @RequestMapping 注释设置值,如下所示: @RequestMapping(值 = VALUE)

@RequestMapping(value = VALUE)
public class SomeClass {
  public final static String value = randomValue();
  public static randomValue() {
     return "some random stuff here";
  }
}

现在,我注意到这是不合法的,因为我收到了这个错误:

Attribute value must be constant

有人能解释一下为什么会这样吗?

Attribute value must be constant

只是意味着您不能为端点分配变量或一些随机值,例如 VALUE。您应该提供一个常量字符串或它的倍数,如下例所示:

@RequestMapping(value = "/value")
public class SomeClass {}

@RequestMapping("/value")
public class SomeClass {}

@RequestMapping(value = {"/value", "/"})
public class SomeClass {}