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"
}
1
  • 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. 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. Commented Jun 3, 2019 at 11:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.