1

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"
}
Ahmad
  • 2,110
  • 5
  • 26
  • 36
  • 1
    The core question is, how does a client know what values to send? If it is contained in some kind of documentation, you aren't doing REST but some kind of RPC. In such a case you might want to have a look into [jsonschema](https://json-schema.org/). If you want to benefit from a REST architecture, you'd probably use some kind of form representation, similar to HTML forms, where you teach a client on the available options and it will chose from these. – Roman Vottner Jun 03 '19 at 11:53

1 Answers1

1

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

mario45211
  • 133
  • 1
  • 7