0

In below json request, you can see salary is available at 3 level student level, trainee level and properties level

employee{
id :"123";
name : "ABC";
salary : 100;
college : "abcd..";
Trainees : {
  id :"101";
  name :"XYZ";
  salary :50;
 }
properties :{ // key value pair values
  default.salary= 20
  default.college=null
 }
}  

As salary is mandatory for my application, so salary should be available at least one level.

If salary field and its value is not available at all level then some error should be thrown and request should not be processed by rest webservice and if salary is available at any one level then request should be accepted and processed by rest webservice.

I am using currently javax.validation.constraints for json request validation, Is there any way to achieve above requirement?

2
  • Could you add your POJOs to the description? Commented Sep 7, 2020 at 14:59
  • What if Jpa is not used? Integrations for example Commented Jan 28, 2023 at 0:18

1 Answer 1

1

If you want to use javax bean validations you will need to define a custom annotation (there is not a default one for you specific use case) and implement ConstraintValidator with your custom validation. You can see examples here: https://www.baeldung.com/javax-validation-method-constraints.

In this case the 3 salary fields should be nullable and your validation logic should verify that at least one of them is not null.

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.