Reputation: 2190
In my REST API, I have a variable which can have only three values "E", "U" or null.
If any other values come-in , I want to through a validation exception or something alike.
Is there anything I can do with json annotations to handle it or any other way?
Below is a sample:
{
"name": "dave",
"Status": "E" , --this can have values only "E" or "U"
}
Upvotes: 1
Views: 1311
Reputation: 143
Solution #1: Check javax.validation to validate list of values?
Solution #2: Write custom validation service, put your allowed values in enum class and check if value from request is correct
This can be done by call to service in your controller or by creating custom Bean Validation annotation - some details can be found here: https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/validator-customconstraints.html#validator-customconstraints-errormessage
//edit Solution #2 also covered in Solution #1
Upvotes: 1