29

I am using spring rest controller.

Here is the code.

@RequestParam(value = "status", required = false, defaultValue = StatusEnum.STATUS.toString())

If i use enum as a defaultValue i am getting The value for annotation attribute RequestParam.defaultValue must be a constant expression.

As per my understanding it should accept enum as a default value.

Please advice.

Chetan Shirke
  • 896
  • 4
  • 13
  • 35

2 Answers2

31

Since it has to be a String, and it has to be a constant expression, your only real option here is to use the value that will work for Enum.valueOf(), since that's how this is eventually resolved.

Specifically, yours should read

@RequestParam(value = "status", required = false, defaultValue = "STATUS")

Assuming, of course, that "STATUS" is the string value for StatusEnum.STATUS.

Ivar
  • 6,138
  • 12
  • 49
  • 61
biggusjimmus
  • 2,706
  • 3
  • 26
  • 31
2

A work around to avoid strings in different location is to use something like that

public enum StatusEnum {
    STATUS();
    
    public class Names {
       public static final String STATUS = "STATUS";
    }
    
 }

And in the query param

@RequestParam(value = "status", required = false, defaultValue = StatusEnum.Names.STATUS)