Join Alex Theedom for an in-depth discussion in this video Implement validation failure management, part of Java EE: RESTful Service with JAX-RS 2.0.
- [Instructor] Okay, so the first thing we need to do is to create an exception mapper. So I'm gonna call my exception mapper constraint violation exception mapper. I'm gonna create it in the exceptions package. So now I'll call it constraint violation exception mapper. This class must now implement the exception mapper interface. So, implements, exception mapper, and it must be past the type of the exception that we want to manage and that's going to be constraint violation exception.
So here I type in constraint violation exception. We must also annotate the class provider. We do this so that the javax runtime knows to pick up this class. Now, let's implement the only method to response. Now, this method receives a reference to the constraint violation exception, which contains the set of constraint violation entities.
I'm going to construct a simple string that concatenates the error messages together. So, to do this, I'm going to use the stream API in Chapter Eight. The first thing I'm going to do is to create a string called message. And then, going to take my exception. I'm going to get constraint violations. This is the set of constraint violations I referred to earlier. I'm going to call stream on this.
And I'm going to use the map feature to create my message. I'm also going to create a helper method that extracts the property name. And I'm going to call this one extractPropertyName. I'm going to write this class in a moment. And all this class needs is to get the stream property. So, from the constraint violation, I simply call getPropertyPath.
I'm going to make it toString. And now I'm simply going to add to this or concatenate to this property name, the actual message value. So, in order to make it look nice, I'm going to use colon to separate the property name from the message. And I'm just going to concatenate onto this the actual message itself. So, I'm going to call the getMessage method. Then, I'm going to collect all of this together using our Collector, and I'm going to join all of this together and separate it with a comma like so.
Okay, I still need to create the extract property name method, which I am going to do now. So, this is going to be a private method that returns the string, which is the property name. And I'm just going to call it like you see above, PropertyName. Now, this accepts a string as a path. And all it does is return the path, substringed, to the path last indexed.
And all this is really doing is just extracting the precise information that I need from the property name. Okay, so now we have our little helper method. So, all I have to do is call the status method on the Response Class as we have done before. So, Response, status. I'm going to give it the status of BAD_REQUEST. So, what I need to do now is to add the custom header.
And I do this, by simply passing to it a name that I want the header to be. Just going to be X-Validation-Failure. So, X-Validation-Failure. And pass it the message, which I constructed above. And then, I just need to build this response by calling the build method, and that's it. So, whenever there is a validation exception, this class will handle it.
And add the violation message to the custom X-Validation-Failure 80 to the header. Now, let's deploy the application and test to see that this works. So, as usual, we need to move into our rest-server package, and execute our maven command, which is always going to be maven, clean, package, cargo, run. Okay, now the application has deployed. Let's go to Postman and test the code that we have just implemented.
Don't forget to press ctrl + C to stop the container. So, what we have here is a post request to the end point that creates a book. And, here we have a payload which gives you the details relating the book. So, what I want to do is to change some of the data in this JSON so that it violates some of the constraints. So, the first thing I want to do is I'm going to change the ID, so it's less than 10. Also, what I'm going to do, is I'm going to change the link so it does not conform to the requirements of a link. I'm just going to call this XXXXX.
Okay. Now, what I should expect to see is two violation constraint exceptions, and those should be captured and turned into a reasonable response which we should see in the header. So, what I'm going to do is I'm going to hit Send, scroll down here to the bottom, and as you can see we have a 400 Bad Request. And also what you can see here is the X-Validation-Failure and the two constraining violations. So, the first one says ID. The ISBN should be 10 characters, and link must match and then it has the regular expression for the link.
This is just one of two ways to create a useful response to the client if there is some violation errors. The other way is to put this same data in the body response.
Join Alex Theedom as he guides you through a wide range of topics, including how to define resource endpoints, how to add dynamism to your application with hypermedia, how to maintain data integrity with Bean Validation, and how to process messages with the JSON Processing API. Once you have completed this course, you'll be ready to develop your own client and server RESTful APIs.
- Discover the extent of JAX-RS's features
- Use the annotation methodology
- Use the inheritance methodology
- Define a REST contract and API root
- Create the REST resource entity
- Create the REST resource methods
- Understand consumers and producers
- Work with different media types
- Define and work with path parameters
- Build a REST response and handle exceptions
- Implement Bean Validation and manage failures
- Create a RESTful client that consumes the web service
- Use JSON-P API with the REST client
- Implement hypermedia (HATEOAS)