Issue
I have the following which works well in receiving a non-empty Array,
$.ajax({
url: "ajaxController",
dataType: "json",
type: "get",
data: {
'term': request.term,
'exclude': ["45","66"]
},
Controller (note the []
in RequestParam Value -- the result goes in a String[]):
public List<KeyValueBean> getChoices(String term,
@RequestParam(value = "exclude[]")
String[] exclude) {
}
But if I pass an empty array in the same code, which sometimes happens, it breaks:
'exclude': []
or alternatively
'exclude': JSON.stringify([])
Error:
org.springframework.web.bind.MissingServletRequestParameterException: Required
String[] parameter 'exclude[]' is not present
Solution
If you pay attention to the error, it says your request parameter exclude
can not be null. If you need to can send empty array sometimes, you can mark your parameter as optional(not required) in this way:
@RequestParam(required = false, value = "exclude[]")
Answered By - hamed
Answer Checked By - Candace Johnson (JavaFixing Volunteer)