Advanced RESTful Validation in Spring Boot with Kotlin
Introduction
Welcome to the course Advanced RESTful Techniques in Spring Boot. In your previous courses, you've covered Spring basics such as Inversion of Control (IoC), Dependency Injection (DI), and managing beans. You also learned how to create simple RESTful applications with CRUD endpoints. This course will take you a step further by exploring advanced topics like request body validation, error handling, pagination, and handling request and response headers. In this lesson, we'll focus on body validation.
Importance of Validation
Imagine you’re developing a check-in form for an airline application. The airline industry is highly regulated, and missing or incorrect information can lead to delays at the check-in counter. Therefore, users must provide their name, surname, age, and must confirm the accuracy of this information by selecting a checkbox.
Here's an example of a valid request body:
And here's an example of an invalid request body:
Methods of Validation
To ensure the integrity and accuracy of the data, various validation approaches can be employed:
-
Client-Side Validation: Here, the frontend application (e.g., built with React) checks the required fields before sending an HTTP request. This approach provides a more responsive user experience since errors are caught instantly. However, it is not foolproof, as users can still bypass it by making direct HTTP requests using tools such as CLI. Additionally, certain validation scenarios, such as verifying a coupon code against a database, cannot be handled solely on the client side.
-
Server-Side Validation: In this approach, the frontend sends the request regardless of the validation status, and the backend handles the validation logic. While this ensures a higher level of security and accuracy, it introduces a delay as users have to wait for the server response to see validation errors.
-
Combined Validation: This method uses both client-side and server-side validation, offering the best of both worlds. It provides immediate feedback to users and ensures the data’s integrity and security.
