Discover the Bean Validation API from the Java EE ecosystem, what is it and how to use it. In this video, learn about all the headline features of the Bean Validation API and what they are used for and where in your application they can be used.
- [Instructor] Bean validation is about ensuring the integrity of data in an application as it is entered, retrieved, stored, and used by the business function of the application. Imagine this scenario. Your website has a Newsletter Sign Up form so the visitors can sign up to receive news about your products, services, and special offers. A visitor only needs to enter an email address and their name and hit the Submit button to subscribe. When he or she does so, the personal information in the form is passed to the server and held in a JavaBean like the one you see on the screen now.
As you can see, it has a field for name and one for email. Then the data in the JavaBean is persisted in a table in the website's database and later on it is retrieved and the email address and visitor's name are used to send a newsletter. This is an important business function and one that we all are very familiar with. It is a very effective way to engage visitors and customers, increase sales, and promote customer loyalty, therefore, it is important to get it right. An important part of getting it right is to ensure that the name and email address that the visitor enters is entered correctly and this is where Bean validation comes in.
Bean validation ensures that the data entered by the visitor is correct. It can ensure that the visitor did actually enter his or her name and that the email address conforms to the correct format for an email address, that is, that it has an at symbol, contains valid characters, and a valid domain. And if it doesn't validate to the correct format, the data is rejected and a message is sent back to the visitor, giving them a chance to enter the correct information. This is just one example of how Bean validation can help us deliver business functionality that ensures data integrity.
Your application expects data to have a certain shape and assumptions are made based on that shape always being the same, which means that if data does not conform, it is misshapen and bad things can happen that will make your application unstable. This is not a good experience for end users and could have serious business and legal consequences as well, so validating the data that enters your application is an essential function that you as a developer must ensure happens. Imagine you're developing casino website and only those over the age of 18 are allowed to gamble in the country that the website will operate.
You must ensure that when a new customer signs up that their age is equal to or greater than 18. If not and someone under 18 entered the website, there could be very serious consequences for you and your business. The Bean validation API contains mechanisms that you can use to implement this business rule, so to protect yourself, your business, and your customers. Bean validation does this with what's called Bean validation constraints. During this course, you'll be learning all you need to know and a little bit more about how to implement this important function into your application. Bean validation constraints encapsulate business rules about data expectations that you want to ensure remain true throughout the life of the data.
Data doesn't just enter your application from the user interface, it can enter from any system via, say, a Quote or REST endpoint from a legacy enterprise information system or imported from a flat file from a CSV file and at all of these entry points, Bean validation can be used to ensure that data conforms to the right shape. For example, the email address is always a valid email address. Take a look at this graphic. It shows a typical three-tiered application. It has a presentation layer, a business layer, and a data persistence layer. At each of these layers, Bean validation can be used to enforce data-related business rules.
For example, at the presentation layer, we can use JSF where backing beans are used to capture information inputted by the user. Here Bean validation can be used to ensure that the data is passed to the backing bean and has been entered correctly. At the business layer, data can be received from a REST endpoint using JAX-RS where Bean validation can be used to validate data received from another system. And at the persistence layer, we use JPA to persist and retrieve data from a database, and again, Bean validation can be used to ensure data conforms to business rules.
And in all of these cases, Bean validation is clearly integrated into each of the respective Java E APIs and provoked automatically, so you don't even need to write any plumping code to make this happen. Every point where data can enter the system can be protected by Bean validation. Once you had decided that you want to use Bean validation in your application, which I hope you will always decided to do, you will need to get on with implementing it, but before you do that, let's have a look at how we used to do Bean validation in the past. This will help us appreciate just how well-designed the Bean validation API really is.
Okay, so let's take an example from earlier on where a visitor can join the website's mailing list. In order to validate an email address, traditionally, we would write code that is pattern matching on the email address using the java.util.regex.Pattern API and might look something like this. There's nothing wrong with this code, it does the job, but would have to appear everywhere within the application where newsletter data might enter. This could be via a web form and therefore, a JSF backing bean, it might also be via a REST endpoint if the application lets third-party sites interact with it, and it might even be imported from an external file if some newsletter requests, for example, are imported from another system.
So, this code will be duplicated in each of these locations and of course, this is error-prone and hard to maintain, especially if the email format needs to be changed for some reason or additional fields are added to the subscription form, and this is where Bean validation comes in. Bean validation uses annotations to mount the fields and parameters where data might enter the system, so a Bean validation is provoked, the field or parameter value is retrieved and validated again rules associated with that annotation. So, in the case of the email field in our example of a newsletter subscription form, the email property of the JavaBean that backs the form will be annotated email.
Now, whenever this bean is processed, the Bean validation mechanism is triggered and the email field value is retrieved and validated to test its conformity to a correctly formatted email. Behind the scenes, the same pattern matching logic that you would code to validate an email the old school way is used to validate the email field value in a Newsletter Sign Up JavaBean, the remaining Bean validation constraints built into the API ready for you to use and I'll be looking at all of them during this course.
In this course, you can journey through the many constraints that are built into the 2.0 version of the Bean Validation API, as well as the additional constraints provided by the Hibernate Validator 6.0. Learn Bean Validation in a practical way while you build a functioning web application. Instructor Alex Theedom shows how to extend the API with custom constraints and how to internationalize validation failure messages. Plus, learn advanced techniques such as cross-field and cross-parameter validation, and how Bean Validation integrates with other Java EE technologies such as JAX-RS, Java Persistence API (JPA), and JavaServer Faces (JSF). By the end of this course, you will have gained practical experience of using Bean Validation in a real web application and be able to implement what you have learned in your own Java applications.
- What is Bean Validation?
- Applying the built-in constraints
- Applying Hibernate constraints
- Designing custom constraint validators
- Managing and configuring validation failure messages
- Creating custom composite constraints
- Creating custom validation constraints
- Cross-field and cross-parameter validation
- Integration with RESTful web services (JAX-RS)