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.
The default value of the @RequestParam is used to provide a default value when the request param is not provided or is empty. In this code, if the person request param is empty in a request, the getName() handler method will receive the default value John as its parameter.
Use Enums as Request Parameters Or we can use it as a PathVariable: @GetMapping("/findbymode/{mode}") public String findByEnum(@PathVariable("mode") Modes mode) { // ... } When we make a web request, such as /mode2str? mode=ALPHA, the request parameter is a String object.
By default, @RequestParam requires query parameters to be present in the URI. However, you can make it optional by setting @RequestParam 's required attribute to false . In the above example, the since query param is optional: @RequestParam(value="since", required=false) ).
Optional Request ParametersMethod parameters annotated with @RequestParam are required by default.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With