how can I limit @RequestParam array size in my endpoint ?? Is there any annotation for that ?
What I'm looking for is that I can send request with at most 5 parameters:
how can I limit @RequestParam array size in my endpoint ?? Is there any annotation for that ?
What I'm looking for is that I can send request with at most 5 parameters:
You need to handle this in your server code. Take an array of request parameter and check on size. I do not know any annotation for that.
If it is not important how the parameter (keys) should be called you can use a Map<K,V>
as @RequestParam
Limitation of a map entry is not possible through spring (found nothing about it). I also tried to add a Wrapper around it and Validate it with the annotation @Size, also not possible.
Solution you can use: Make an iterator out of the Map which is coming from the request as parameter and afterwards adding the entries to a new map.
@RequestMapping(value = "/test",method = RequestMethod.GET)
public Object test(@RequestParam Map<String,String> map){
Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
int counter = 0; //To limit the parameter
Map<String,String> resultMap = new HashMap<>(); //Our result map which we will return
while (entries.hasNext() && counter < 5) { //iterate over map and stop at five entries
Map.Entry<String, String> entry = entries.next(); //get next entry
resultMap.put(entry.getKey(),entry.getValue()); //Put the entry in our resultMAp
counter++; //count up
}
return resultMap;
}