How to handle validating input with JAX-RS

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
<dependency> <- Used to import junit classes
<dependency> <- Used to import validation classes, used during testing
<dependency> <- Required for hibernate-validator
<dependency> <- Reguired for hibernate-validator
<dependency> <- I'm using Lombok, but makes no difference to JavaEE validation

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 —



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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Reinhardt van Wyk

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