Applying Design by Contract – adding validation
In this section, we're going to take a look at adding validation to the @PathParam
annotation using Javax validation annotations. We will be adding validation POJOs and POST
bodies using Javax validation annotations. I'm going to show you how to send the HTTP 404 status code for bad and invalid requests.
Let's switch to our IDE. As usual, we will prepare a small template project to get started. We create a BookResource
similar to the one created in the previous Chapter 5, Using Server-Sent Events (SSEs). There's one thing missing though: there's nothing that tells the API that the ISBN is valid. Let's assume we wanted to add a validation for the ISBN and make sure that it's always 10-characters long and that it only contains digits. Of course, we could program this manually, but there's a better approach.
Instead, we can use Javax validation for this. Let's do that by adding the @Pattern
annotation. If you hover over the @Pattern
annotation in...