Spring Boot - MVC Form Input Validation using JSR 303/349/380 Bean Validation annotations

[Updated: Jun 26, 2018, Created: Jun 26, 2018]

Following example shows how to use JSR 303/349/380 Bean Validation API in Spring Boot. The implementation of Bean Validation API should be on the classpath; we are using hibernate-validator in this example. We are also going to use Thymeleaf views.


Using validation annotations

package com.logicbig.example;

import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.time.LocalDate;

public class Employee {
  @Size(min = 5, max = 50)
  private String name;
  @Pattern(regexp = "Admin|IT|Sales|Accounts")
  private String dept;
  @DateTimeFormat(pattern = "yyyy-MM-dd")
  private LocalDate dateOfBirth;

In above bean we are also using spring specific @DateTimeFormat annotation on 'dateOfBirth' field. This annotation is used by the Spring conversion service to convert the input string to the date object and vice-versa. After conversion Bean validation annotations are used to performed the validations.

MVC Controller

public class EmployeeController {
  //todo replace this with employee DAO/repository.
  private static List<Employee> employeeList = new ArrayList<>();

  public String handlePostRequest(@Valid Employee employee, BindingResult bindingResult) {
      if (bindingResult.hasErrors()) {
          return "employee-form";
      return "redirect:/employees";

  public String handleGetRequest(Employee employee) {
      return "employee-form";

  public String handleGetRequest(Model model) {
      model.addAttribute("employees", employeeList);
      return "employee-view";

Thymeleaf views


table.emp-form td:nth-child(3){color:red;}
<form action="#" th:action="@{/}" th:object="${employee}" method="post">
    <table class="emp-form">
            <td><input type="text" th:field="*{name}"/></td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
            <td><input type="text" th:field="*{dept}"/></td>
            <td th:if="${#fields.hasErrors('dept')}" th:errors="*{dept}">Department Error</td>
            <td>Date Of Birth:</td>
            <td><input type="text" th:field="*{dateOfBirth}"/></td>
            <td th:if="${#fields.hasErrors('dateOfBirth')}" th:errors="*{dateOfBirth}">Date Of Birth Error</td>
                <button type="submit">Submit</button>


table.emp-table td {border:solid 1px #aaa;}
table.emp-table th {border:solid 1px #aaa; background: #bbb;}
    <h3>Saved Employees</h3>
    <table class="emp-table">
            <th>Date Of Birth</th>
        <tr th:each="employee : ${employees}">
            <td th:text="${}"></td>
            <td th:text="${employee.dept}"></td>
            <td th:text="${employee.dateOfBirth}"></td>
    <a th:href="@{/}">Add new employee</a>

Main class

public class ExampleMain {

  public static void main(String[] args) {;


To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

Or run the main method class from IDE.


Accessing http://localhost:8080/ and entering valid values:

On submitting the form:

On entering invalid values and submitting the form:

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.0.3.RELEASE
    Corresponding Spring Version 5.0.7.RELEASE
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • spring-boot-starter-thymeleaf : Starter for building MVC web applications using Thymeleaf views.
    Uses org.thymeleaf:thymeleaf-spring5 version 3.0.9.RELEASE
  • hibernate-validator 6.0.10.Final: Hibernate's Bean Validation (JSR-380) reference implementation.
  • JDK 1.8
  • Maven 3.5.4

Spring Boot - Form Validation Example Select All Download
  • spring-boot-mvc-form-input-validation
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • resources
            • templates

    See Also