Issue
By default Spring MVC assumes @RequestParam
to be required. Consider this method (in Kotlin):
fun myMethod(@RequestParam list: List<String>) { ... }
When passing empty list from javaScript, we would call something like:
$.post("myMethod", {list: []}, ...)
In this case however, as the list is empty, there is no way to serialize empty list, so the parameter essentially disappears and so the condition on required parameter is not satisfied. One is forced to use the required: false
on the @RequestParam
annotation. That is not nice, because we will never receive the empty list, but null.
Is there a way to force Spring MVC always assume empty lists in such case instead of being null
?
Solution
To get Spring to give you an empty list instead of null
, you set the default value to be an empty string:
@RequestParam(required = false, defaultValue = "")
Answered By - Laplie Anderson