javaspring-bootcompiler-errorsstaticrequest-mapping

Request mapping and static fields


I am trying to set value for @RequestMapping annotation as follows: @RequestMapping(value = VALUE)

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

Now, I've noticed that is not legit since I got this error:

Attribute value must be constant

Can someone explain me why this is happening, please?


Solution

  • Attribute value must be constant

    Simply means that you can not assign a variable or some random values like VALUE for an endpoint. You should supply a constant string or multiple of it like the examples below:

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