How to handle validating input with JAX-RS

Ryan Vanwyk
3 min readOct 9, 2020

In any webservice, validation input is extremely important because it helps keep the data consistent and secure. During my years as a Java developer, I’ve written several validations within the code, but I have found that this distracts from the intent of the business flow. Creating validation within the business code also creates several messy logical flows which inherits the “{“ and the “}”.

JavaEE (6 & up) provides a mechanism that allows us to embed the validation out of sight through the use of annotations.

You will require the JavaEE dependency. I will be using Maven.

<dependency> <- Used to import javax validation
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency> <- Used to import junit classes
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency> <- Used to import validation classes, used during testing
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.6.Final</version>
</dependency>
<dependency> <- Required for hibernate-validator
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency> <- Reguired for hibernate-validator
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency> <- I'm using Lombok, but makes no difference to JavaEE validation
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

The javaee-api allows us to leverage the contents that are available within the constraints package: javax.validation.constraints

I’ve created a sample class called Registration. The purpose of this class is to handle registering the student for a list of classes. The requirements are:

  1. StudentID cannot be null, can only contain number, and can only contain nine characters
  2. StudentName cannot be null, can only contain alpha characters and spaces, and cannot be greater than 50 characters
  3. You need a minimum of 1 class to register, but cannot register for more than 6 classes at a time
  4. Semester start date has to be present and in the future
  5. Tuition cannot be null, 0 can be accepted, but cannot be more than $999,999.99. (we will assume this is calculated by the front-end)

I’m using Lombok, so Getter & Setter annotations are used.

I’m using a few annotations:

  1. NotNull — Checks to ensure that the variable is present and not null
  2. Pattern — Regex pattern that validates the string matches the pattern
  3. Size — Contains a min and/or max size that the string can be
  4. Future — Checks if the date is in the future
  5. Digits — Validates the field as a digit, checking the integer size along with the decimal size

When these annotations are used alone, without the message attribute, default error messages are not thrown. You will notice that I’m using the message attribute within each annotation. This will tell the validator, what message to throw when the validation fails.

Thanks for reading, this is my first post, and I hope it was informative. Check out the source code at the following link — https://github.com/rvanwyk9191/javaxvalidation

--

--

Ryan Vanwyk

Java API & Microservice Developer for the last 8 years. Currently consulting and tinkering with new technologies.