Bean Validation
Bean validation has always been a tremendous effort in Java based Enterprise application development. Java Bean Validation (JSR 303) is the framework that defines how Java Beans should be validated. There are few validator APIs that support JSR 303. Hibernate validator is the most popular among them. It helps validate Java Beans using annotation and the beans can be validated at presentation layer, service and data access layer. Hibernate validator also offers custom validation, cross field validations. You can checkout the latest hibernate validator documentation for built-in validator annotations.
Assumption : Spring Boot (1.5.6), Hibernate Validator(5.4.1.Final), Maven Projects
Integrating Spring Boot & Hibernate Validator
Spring Boot offers built-in starter dependency management for most of the frameworks and APIs available in Java Application Development. It has support for bean validation using hibernate validator API. The discussion focuses on how to validate presentation layer by Hibernate Validator using Spring Boot.
Step 1: Spring Boot has a starter validation that inherits hibernate validator. All we need to do is to add this starter dependency in pom.xml for spring boot application.
As soon as we add this dependency, Spring Boot will download and be aware of hibernate validator and we can use it to validate the beans in presentation layer which is Spring MVC in our case.
Step 2: Enable constraint annotations on Java Beans. Below example shows how to add annotations to validate the constraint on the fields. The naming of the message will be always ConstraintName.BeanName.PropertyName.
Step 3: To validate the form (Java Bean) in presentation layer (Spring MVC), we have to add @Valid annotation in the controller mapping like below. That will do all the tricks to validate the field constraints. Pretty simple. isn't it?
Hibernate Validator has only limited built-in constraints but it has extensibility. We can create our own constraints to suit the needs.
Bean validation has always been a tremendous effort in Java based Enterprise application development. Java Bean Validation (JSR 303) is the framework that defines how Java Beans should be validated. There are few validator APIs that support JSR 303. Hibernate validator is the most popular among them. It helps validate Java Beans using annotation and the beans can be validated at presentation layer, service and data access layer. Hibernate validator also offers custom validation, cross field validations. You can checkout the latest hibernate validator documentation for built-in validator annotations.
Assumption : Spring Boot (1.5.6), Hibernate Validator(5.4.1.Final), Maven Projects
Integrating Spring Boot & Hibernate Validator
Spring Boot offers built-in starter dependency management for most of the frameworks and APIs available in Java Application Development. It has support for bean validation using hibernate validator API. The discussion focuses on how to validate presentation layer by Hibernate Validator using Spring Boot.
Step 1: Spring Boot has a starter validation that inherits hibernate validator. All we need to do is to add this starter dependency in pom.xml for spring boot application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
As soon as we add this dependency, Spring Boot will download and be aware of hibernate validator and we can use it to validate the beans in presentation layer which is Spring MVC in our case.
Step 2: Enable constraint annotations on Java Beans. Below example shows how to add annotations to validate the constraint on the fields. The naming of the message will be always ConstraintName.BeanName.PropertyName.
public class EmployeeForm implements Serializable {
private String id;
@Size(min = 1, max = 50, message = "{Size.employeeForm.firstName}")
private String firstName;
@Size(min = 1, max = 50, message = "{Size.employeeForm.lastName}")
private String lastName;
@NotBlank(message = "{NotBlank.employeeForm.dob}")
private String dob;
@NotBlank(message = "{NotBlank.employeeForm.gender}")
private String gender = Gender.MALE.getCode();
@NotBlank(message = "{NotBlank.employeeForm.email}")
@Email(message = "{Email.employeeForm.email}")
private String email;
}
Step 3: To validate the form (Java Bean) in presentation layer (Spring MVC), we have to add @Valid annotation in the controller mapping like below. That will do all the tricks to validate the field constraints. Pretty simple. isn't it?
@PostMapping(value = "/employees/create.mk")
public String createEmployees(@Valid @ModelAttribute("newEmployeeForm")
EmployeeForm employeeForm, BindingResult result, ModelMap model) {
if (result.hasErrors()) {
model.addAttribute("employeeFormError", Boolean.TRUE);
return ADD_EMPLOYEES;
}
return "redirect:/employees/view.mk";
}
Hibernate Validator has only limited built-in constraints but it has extensibility. We can create our own constraints to suit the needs.
This information is meaningful and magnificent which you have shared here. I am impressed by the details that you have shared in this post and It reveals how nicely you understand this subject. I would like to thanks for sharing this article here. Custom Website Development Services.
ReplyDeleteสล็อต miami1688 หนึ่งในผู้ให้บริการเว็บไซต์รวมเกมสล็อตทุกค่าย เข้าระบบ pg slot สล็อต กับพวกเรา เก็บรวบรวมผู้ให้บริการ ค่ายสล็อตเว็บไซต์ตรง มาไว้ให้บริการตรงนี้ pg-slot.game
ReplyDeleteufa wallet slot สำรวจโลกของยูฟ่าวอลเล็ตสล็อตที่ทำให้ความบันเทิงรวมถึงความตื่นเต้น! ค้นพบเคล็ดลับที่ดีที่สุด PG วิธีการและกลยุทธ์ในการเพิ่มประสบการณ์ในการเล่นเกมของคุณในขณะ
ReplyDelete