hibernate validator Java java bean validation jbvext jsr380 Spring spring boot

Writing an unified validation mechanism for REST APIs using Spring Boot and JSR-380 – andreINC

JSR-380 defines a metadata mannequin and API for Java Bean validation. It can be used an “architectural-agnostic” means and it’s notably helpful relating to validating the RESTful APIs (syntactic validation).

The default metadata supply is @Annotations, with the power to override and prolong the metadata via using XML validation descriptors or custom code:

JSR-380 was completed in August, 2017 and it’s now a Java EE eight normal. It’s additionally included by default in Spring Boot via the only obtainable implementation – Hibernate Validator.

By default, JSR-380 doesn’t standardise an enormous record of validating @Annotations. Hibernate Validator adds a number of additional, and enhances the prevailing ones, nevertheless it’s still not sufficient. As a developer working for an actual world software you will in all probability want to put in writing customized constraints.

That’s why i’ve compiled myself a library of widespread annotations (that are not in the standard or in Hibernate Validator). The library and code might be discovered on github underneath the identify JBVExt (Java Bean Validation Extensions).

The venture setup

The “example” challenge is an easy Spring Boot challenge. The primary dependencies are:

By default the spring-boot-starter-web consists of Hibernate Validator, so there’s no have to explicitly define it as a dependency.

Decorating the RESTful API

The API we are going to validate consists by two REST web-services:

– POST /consumer/ : For creating an consumer;
– POST /submit/ : For submitting the textual content;

The requestBodies are being mapped to the following two courses.

The comments describe each of the field-level constraints.

For instance within the above class the sector appCode needs to all the time begin with “A” and finish in “00”.

Observe: The @Knowledge, @AllArgsConstructor and @NoArgsConstructor usually are not related with JSR-380, but are part of Venture Lombok.

To be able to implement the constraints at the subject degree we might normally to write down customized Java code. This sort of code can turn into repetitive (and less readable) as soon the appliance grows.

JSR-380 proposes a new “decorative” strategy. So as an alternative to add the validation code on the controller degree, we might simply beautify the mannequin layer with @Annotations.

In conclusion the CreateUserReq.java becomes:

And the CreatePostReq.java class becomes:

Creating an unified method of intercepting validation errors.

The JBVExt library accommodates an util technique SimpleValidation.validate(…) that may throw a BeanValidationException exception the primary time a constraint is violated.

So we’ll make each controller of our API invoke this technique earlier than doing anything:

The subsequent step might be define a “Global” Exception Handler that may intercept any BeanValidationException thrown from our Controllers and treat them in the identical manner.

The @ControllerAdvice and @ExceptionHandler annotations (part of Spring) will turn out to be useful:

So as an alternative of having to repeat all of the attempt/catch code in every controller, we will outline this unified strategy in a separate class, that may menace every exception (of sort BeanValidationException) in the same method.

Operating the code

The code is obtainable on git:

The Spring Boot software will run by default on port 8080.

If we attempt to POST http://localhost:8080 with an invalid bodyRequest (as described by the JSR380 validation):