<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eric Cabrel TIOGO</title>
    <description>The latest articles on DEV Community by Eric Cabrel TIOGO (@tericcabrel).</description>
    <link>https://dev.to/tericcabrel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F214643%2F9978e9ed-2646-4ef0-a098-55ffbb805b74.jpg</url>
      <title>DEV Community: Eric Cabrel TIOGO</title>
      <link>https://dev.to/tericcabrel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tericcabrel"/>
    <language>en</language>
    <item>
      <title>Validate Request Body and Parameter in Spring Boot</title>
      <dc:creator>Eric Cabrel TIOGO</dc:creator>
      <pubDate>Sat, 07 Aug 2021 19:17:06 +0000</pubDate>
      <link>https://dev.to/tericcabrel/validate-request-body-and-parameter-in-spring-boot-1fai</link>
      <guid>https://dev.to/tericcabrel/validate-request-body-and-parameter-in-spring-boot-1fai</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://blog.tericcabrel.com/validate-request-body-and-parameter-in-spring-boot/" rel="noopener noreferrer"&gt;https://blog.tericcabrel.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Never trust user input&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A typical workflow of a Web application is: receive a request with some inputs, perform a treatment with the input received, and finally return a response. When developing our application, we usually test only the "happy path" or often think the end-user can't provide bad inputs. To prevent that, we can perform a validation of the inputs before processing the request.&lt;/p&gt;

&lt;p&gt;Spring offers an elegant way to do that, and we will see how to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The use case
&lt;/h2&gt;

&lt;p&gt;We need to build a system where a user can make a reservation for a room in a hotel. The user must provide his address information when registering. The possible actions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Register a user with his address.&lt;/li&gt;
&lt;li&gt;Make a reservation for an existing user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Beneath is the Entity-Relation diagram of the system made with &lt;a href="https://drawsql.app/teco/diagrams/room-reservation" rel="noopener noreferrer"&gt;drawSQL&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fib7dkzrylrwmude2uvo8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fib7dkzrylrwmude2uvo8.png" alt="Minimal entity-relation diagram of a hotel reservation system"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;For this tutorial, you need Java 11 and MySQL installed on your computer or &lt;a href="https://docs.docker.com/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt; if you don't want to install MySQL on your computer. Indeed, you can start a Docker container from MySQL Docker image. It will be enough to achieve the goal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker run -it -e MYSQL_ROOT_PASSWORD=secretpswd -e MYSQL_DATABASE=hotels --name hotels-mysql -p 3307:3306 mysql:8.0


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Setup the project
&lt;/h2&gt;

&lt;p&gt;Let's create a new spring project from &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;start.spring.io&lt;/a&gt; with the required dependencies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc3ri8jazrbiqrck0uc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc3ri8jazrbiqrck0uc3.png" alt="Initialize the Spring Boot project with required dependencies"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The dependency responsible for input validation is &lt;strong&gt;Bean Validation with Hibernate validator&lt;/strong&gt;. Note that this library has no link with Hibernate's persistence aspect, provided here by Spring Data JPA.&lt;br&gt;
Open the project in your IDE and set the server port and database credentials in &lt;code&gt;application.properties&lt;/code&gt; file.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

server.port=4650

spring.datasource.url=jdbc:mysql://localhost:3307/hotels?serverTimezone=UTC&amp;amp;useSSL=false
spring.datasource.username=root
spring.datasource.password=secretpswd

## Hibernate properties
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
spring.jpa.open-in-view=false


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Create entities and services
&lt;/h2&gt;

&lt;p&gt;We need to create the entities &lt;strong&gt;User, Address, and Reservation&lt;/strong&gt;. For each entity, we will create the related Repository and Service. Since it is not the main topic of this tutorial, find the code of these files in the &lt;a href="https://github.com/tericcabrel/blog-tutorials/tree/main/springboot-validation" rel="noopener noreferrer"&gt;Github repository&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Entities are inside the package &lt;strong&gt;models&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Entities Repositories are inside the package &lt;strong&gt;repositories&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Services are inside the package &lt;strong&gt;services&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Register a user
&lt;/h2&gt;

&lt;p&gt;We need to create the endpoint to handle this action and also define the object that will receive the input required to create the user.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create the model for the request body
&lt;/h2&gt;

&lt;p&gt;When registering a new user, we also provide information on his address in the body. We need to structure our object to handle that by creating a class called AddressDTO that will hold all properties for the address, and there will be a property of type AddressDTO inside the class RegisterUserDTO.&lt;/p&gt;

&lt;p&gt;The class names are suffixed with &lt;a href="https://en.wikipedia.org/wiki/Data_transfer_object" rel="noopener noreferrer"&gt;DTO (Data Transfer Object)&lt;/a&gt; because they transport data from one layer (controller) to another layer (persistence). A common use case of his usage is when we need to apply some transformation data before passing them to the other layer. I will write a more detailed post about it later.&lt;/p&gt;

&lt;p&gt;Create a package called &lt;strong&gt;dtos&lt;/strong&gt; inside the package &lt;strong&gt;models&lt;/strong&gt;, then create two classes &lt;strong&gt;AddressDto.java&lt;/strong&gt; and &lt;strong&gt;RegisterUserDto.java&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Add validation
&lt;/h2&gt;

&lt;p&gt;Hibernate Validator provides built-in constraints that will use to validate our input. To see the full list check out this &lt;a href="https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-builtin-constraints" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AddressDto.java&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tericcabrel.hotel.models.dtos;

import com.tericcabrel.hotel.models.Address;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Pattern.Flag;
import lombok.Data;

@Data
public class AddressDto {
  @NotBlank(message = "The country is required.")
  private String country;

  @NotBlank(message = "The city is required.")
  private String city;

  @NotBlank(message = "The Zip code is required.")
  @Pattern(regexp = "^\\d{1,5}$", flags = { Flag.CASE_INSENSITIVE, Flag.MULTILINE }, message = "The Zip code is invalid.")
  private String zipCode;

  @NotBlank(message = "The street name is required.")
  private String street;

  private String state;

  public Address toAddress() {
    return new Address()
        .setCountry(country)
        .setCity(city)
        .setZipCode(zipCode)
        .setStreet(street)
        .setState(state);
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;RegisterUserDto.java&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tericcabrel.hotel.models.dtos;

import com.tericcabrel.hotel.models.User;
import java.util.Date;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern.Flag;
import javax.validation.constraints.Size;
import lombok.Data;

@Data
public class RegisterUserDto {
  @NotEmpty(message = "The full name is required.")
  @Size(min = 2, max = 100, message = "The length of full name must be between 2 and 100 characters.")
  private String fullName;

  @NotEmpty(message = "The email address is required.")
  @Email(message = "The email address is invalid.", flags = { Flag.CASE_INSENSITIVE })
  private String email;

  @NotNull(message = "The date of birth is required.")
  @Past(message = "The date of birth must be in the past.")
  private Date dateOfBirth;

  @NotEmpty(message = "The gender is required.")
  private String gender;

  @Valid
  @NotNull(message = "The address is required.")
  private AddressDto address;

  public User toUser() {
    return new User()
        .setName(fullName)
        .setEmail(email.toLowerCase())
        .setBirthDate(dateOfBirth)
        .setGender(gender)
        .setAddress(address.toAddress());
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Create a route to test registration
&lt;/h2&gt;

&lt;p&gt;Let's create an endpoint responsible for registering a new user. Create a package called &lt;strong&gt;controllers&lt;/strong&gt;, then create a controller called &lt;strong&gt;UserController.java&lt;/strong&gt;. Add the code below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tericcabrel.hotel.controllers;

/*      CLASSES IMPORT HERE        */

@RequestMapping(value = "/user")
@RestController
public class UserController {
  private final UserService userService;

  public UserController(UserService userService) {
    this.userService = userService;
  }

  @PostMapping("/register")
  public ResponseEntity&amp;lt;User&amp;gt; registerUser(@Valid @RequestBody RegisterUserDto registerUserDto) {
    User createdUser = userService.create(registerUserDto.toUser());

    return new ResponseEntity&amp;lt;&amp;gt;(createdUser, HttpStatus.CREATED);
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The most important part of the code above is the use of the &lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/valid"&gt;@valid&lt;/a&gt;&lt;/strong&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When Spring finds an argument annotated with &lt;a class="mentioned-user" href="https://dev.to/valid"&gt;@valid&lt;/a&gt;, it automatically validates the argument and throws an exception if the validation fails&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Run the application and make sure there is no error at the launch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test with postman
&lt;/h2&gt;

&lt;p&gt;Our app launched; open postman and send a request with all the input to null and see the result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnv4yxxhmyhr9xchfbmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcnv4yxxhmyhr9xchfbmv.png" alt="Register user with invalid information"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We got an HTTP status 400 with the message "Bad Request," nothing more 🙁. Let's check the application console to see what happened:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foflyhvlcv70wlsdxrazx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foflyhvlcv70wlsdxrazx.png" alt="Validation error message printed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, an exception of type &lt;strong&gt;MethodArgumentNotValidException&lt;/strong&gt; has been thrown, but since the exception is not caught anywhere, the response fallback to a Bad Request. &lt;/p&gt;

&lt;h2&gt;
  
  
  Handle validation error exception
&lt;/h2&gt;

&lt;p&gt;Spring provides a specialized annotation of &lt;strong&gt;@Component&lt;/strong&gt; called &lt;strong&gt;@ControllerAdvice&lt;/strong&gt; which allows handling exceptions thrown by methods annotated with &lt;strong&gt;@RequestMapping&lt;/strong&gt; and similar in one global single component.&lt;/p&gt;

&lt;p&gt;Create a package called &lt;strong&gt;exceptions&lt;/strong&gt;, then create a file called &lt;strong&gt;GlobalExceptionHandler.java&lt;/strong&gt;. Add the code below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tericcabrel.hotel.exceptions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
  @Override
  protected ResponseEntity&amp;lt;Object&amp;gt; handleMethodArgumentNotValid(
      MethodArgumentNotValidException ex, HttpHeaders headers,
      HttpStatus status, WebRequest request) {

    Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; body = new HashMap&amp;lt;&amp;gt;();

    List&amp;lt;String&amp;gt; errors = ex.getBindingResult()
        .getFieldErrors()
        .stream()
        .map(DefaultMessageSourceResolvable::getDefaultMessage)
        .collect(Collectors.toList());

    body.put("errors", errors);

    return new ResponseEntity&amp;lt;&amp;gt;(body, HttpStatus.BAD_REQUEST);
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Launch the app and make the call on Postman.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq372fg9jft9ohokl6c46.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq372fg9jft9ohokl6c46.png" alt="Validation error returned are readable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Test birth date in the feature and the ZIP code with alphabetical letters (The ZIP code in France can't have letters).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvwf5knz6i38otcwj5j3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvwf5knz6i38otcwj5j3x.png" alt="Nested validation in action"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a reservation
&lt;/h2&gt;

&lt;p&gt;Let's do the same by creating &lt;strong&gt;CreateReservationDto.java&lt;/strong&gt; then add the code below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.tericcabrel.hotel.models.dtos;

import com.tericcabrel.hotel.models.Reservation;
import java.util.Date;
import javax.validation.constraints.FutureOrPresent;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import lombok.Data;

@Data
public class CreateReservationDto {
  @NotNull(message = "The number of bags is required.")
  @Min(value = 1, message = "The number of bags must be greater than 0")
  @Max(value = 3, message = "The number of bags must be greater than 3")
  private int bagsCount;

  @NotNull(message = "The departure date is required.")
  @FutureOrPresent(message = "The departure date must be today or in the future.")
  private Date departureDate;

  @NotNull(message = "The arrival date is required.")
  @FutureOrPresent(message = "The arrival date must be today or in the future.")
  private Date arrivalDate;

  @NotNull(message = "The room's number is required.")
  @Positive(message = "The room's number must be greater than 0")
  private int roomNumber;

  @NotNull(message = "The extras is required.")
  @NotEmpty(message = "The extras must have at least one item.")
  private String[] extras;

  @NotNull(message = "The user's Id is required.")
  @Positive(message = "The user's Id must be greater than 0")
  private int userId;

  private String note;

  public Reservation toReservation() {
    return new Reservation()
        .setBagsCount(bagsCount)
        .setDepartureDate(departureDate)
        .setArrivalDate(arrivalDate)
        .setRoomNumber(roomNumber)
        .setExtras(extras)
        .setNote(note);
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Find the code for &lt;strong&gt;ReservationController.java&lt;/strong&gt; in the source code repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test with postman
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3vyye0y1shwtzyt67nk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe3vyye0y1shwtzyt67nk.png" alt="Create a reservation with bad input"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Validate Request parameter
&lt;/h2&gt;

&lt;p&gt;Now, we want to retrieve a reservation through the generate unique code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07xxj9fxysgfbotgzit4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07xxj9fxysgfbotgzit4.png" alt="List of all reservations in the database"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The endpoint will look like: &lt;strong&gt;/reservations/RSV-2021-1001&lt;/strong&gt;. Since the reservation's code is provided by the user, we need to validate it to avoid making an unnecessary database call cause if the code provided is not in this good format, we are sure it will not be found in the database.&lt;/p&gt;

&lt;p&gt;When creating a route with Spring, it's possible to add an annotation rule to validate the input. In our case, we will apply a Regex the validate the format of the reservation's code.&lt;/p&gt;

&lt;p&gt;Inside the &lt;strong&gt;ReservationController.java&lt;/strong&gt;, add the code below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@GetMapping("/{code}")
public ResponseEntity&amp;lt;Reservation&amp;gt; oneReservation(@Pattern(regexp = "^RSV(-\\d{4,}){2}$") @PathVariable String code)
      throws ResourceNotFoundException {
    Optional&amp;lt;Reservation&amp;gt; optionalReservation = reservationService.findByCode(code);

    if (optionalReservation.isEmpty()) {
      throw new ResourceNotFoundException("No reservation found with the code: " + code);
    }

    return new ResponseEntity&amp;lt;&amp;gt;(optionalReservation.get(), HttpStatus.OK);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you run the application and call the route with a bad code, nothing happens because we need to tell the controller to validate parameters with annotation rules. We achieve that with the annotation &lt;strong&gt;@Validated&lt;/strong&gt;, which is added on the ReservationController class:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@Validated
@RequestMapping(value = "/reservations")
@RestController
public class ReservationController {
    // code here
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now run the application and test with a bad reservation code. &lt;/p&gt;

&lt;p&gt;Ooops!!! The application throws an internal server error, and the console looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsijqnmaksf5pmjt2rcoq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsijqnmaksf5pmjt2rcoq.png" alt="Constraint validation exception"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The validation failed as expected, but a new exception of type &lt;strong&gt;ConstraintViolationException&lt;/strong&gt; has been thrown. Since it is not caught by the application, an internal server error is returned. Update the &lt;strong&gt;GlobalExceptionHandler.java&lt;/strong&gt; to catch this exception:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity&amp;lt;?&amp;gt; constraintViolationException(ConstraintViolationException ex, WebRequest request) {
    List&amp;lt;String&amp;gt; errors = new ArrayList&amp;lt;&amp;gt;();

    ex.getConstraintViolations().forEach(cv -&amp;gt; errors.add(cv.getMessage()));

    Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; result = new HashMap&amp;lt;&amp;gt;();
    result.put("errors", errors);

    return new ResponseEntity&amp;lt;&amp;gt;(result, HttpStatus.BAD_REQUEST);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Launch the application and test. Now we got an error with a clear message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgse3airz16dkkbna1qp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgse3airz16dkkbna1qp.png" alt="Constraint validation error message is readable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We reached the end of this tutorial, and now we can implement a solid validation that made our backend more resilient to bad inputs coming from users. We used predefined validation rules throughout the tutorial, but we can also create our custom validation rule for a specialized use case. &lt;/p&gt;

&lt;p&gt;To see how to write our own validation rule, check out &lt;a href="https://blog.tericcabrel.com/write-custom-validator-for-body-request-in-spring-boot/" rel="noopener noreferrer"&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Find the code of this tutorial in the &lt;a href="https://github.com/tericcabrel/blog-tutorials/tree/main/springboot-validation" rel="noopener noreferrer"&gt;Github repository&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>java</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
