Is it possible to set possible values for @RequestParam(value = "id") String id)
?
The ideal is: I give some List<String> allowedValues
and it automatically validate it. (This list will be loaded from database).
Is it possible to set possible values for @RequestParam(value = "id") String id)
?
The ideal is: I give some List<String> allowedValues
and it automatically validate it. (This list will be loaded from database).
use an enum instead of a String and define your enum values
@RequestMapping(value = "yourRestEndPoint", method = RequestMethod.GET)
public anyReturnType methodName(@RequestParam(defaultValue = "ONE") IdValue id) {
....
}
and have a separate class defined e.g
public enum IdValue {
ONE, TWO, THREE
}
It is possible but not some magical way but by simple Validation checks.
Eg:
@RequestMapping(value = "yourRestEndPoint", method = RequestMethod.GET)
public anyReturnType methodName(@RequestParam(value = "id") String id){
List<String> allowedValues = getAllowedValuesFromDB();
if(allowedValues.contains(id)){//check if incoming ID belongs to Allowed Values
//Do your action....
}
}
private List<String> getAllowedValuesFromDB(){
List<String> allowedValues = new ArrayList<>();
//fetch list from DB
//set fetched values to above List
return allowedValues;
}
Second Way
If you want to do it like we do Bean Validation, using @Valid, which is for Bean not just a single parameter and also required Validator to be configured, then check out this & this answer.
Hope this helps.