2
votes

I have built a Spring REST service (Spring 4.0 and Java 8) and I have a Employee REST service where in the Employee class has the following attributes name, age, sex, salary, designation, company_id ,holiday_entitlement, manager_name & country.

I am looking to write a validation layer using Spring 4.0 , the validation needs to be dynamic based on the country of the employee

Validations Expected

  1. Employee name, age, sex and country is mandatory.

  2. Employee Sex has to be Male or Female

  3. Employee age cannot be greater than 70 years and not lesser than 18 years.

  4. For example if the country is UK, the holiday entitlement cannot be greater than 25 days If the country is US, the holiday entitlement cannot be greater than 20 days If the country is Spain, the holiday entitlement cannot be greater than 30 days

  5. Similarly, if the country is UK, then it can accept a designation from only a predefined list of designations for UK, similar approach for other countries.

  6. If the manager_name is provided , then I need to validate that he is part of the same company id as the employee. So I will have to lookup some DAO to get the manager's company ID.

Questions

A. Given the above validation requirements, I would like to understand if there are any specific classes in Spring that will allow dynamic validations such as based on country.

B. When would the validations be called, immediately post my JSON gets converted to a Java object by spring or within the Services layer (class annotated @Service).

C. Should I still use spring bean validations JSR 303 annotations in my Employee POJO or should the validations happen in a different class esp for attributes that are mandatory?

2

2 Answers

2
votes

A: As far I know there is no special validation layer in Spring, javax.validation is used for which Hibernate is the reference implementation. What's really easy to implement is method validation, as an example you could have a look at https://github.com/flexguse/validation-violation-checker-demo.

B: You can do the JSR303 validation directly in the Controller method by using @Valid method annotation but I would prefer to do the validation in your service method so you have more control what is responded in case of validation errors -> see https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

C: JSR 303 annotations should be enough, you need to create your own validation annotation and validation implementation which you should add to the Employee POJO -> see https://softwarecave.org/2014/03/27/custom-bean-validation-constraints/

Do not forget to write JUnit tests for your validation!

0
votes

I don't think there is a special validation Layer responding 100% to your need, but for example, in Spring MVC, we have a validator interface org.springFramework.validation.Validator.

It give you the possibility to define dynamically the rules to validate the data using the validate method and invoke it by creating a binder and @Valid in our controller.