Multiple validations using spring validation

Multiple validations using spring validation

Spring facilitates request validation by means of Spring Validation which enables us to bind a validator to a controller.

Spring facilitates request validation by means of Spring Validation which enables us to bind a validator to a controller.

With Spring based validations the validator for a controller is specified by annotating the method which accepts an argument of type WebDataBinder and a void return type with @Initbinder in the controller.

Image for post

However this approach comes with its own set of limitations. I will hereby elaborate on the possible problems and the ways in which you can circumvent them.

Everything seems to work fine as long as the controller has a single endpoint accepting request of type RegistrationForm or multiple endpoints with the same type of request.

But when we have two different endpoints with two distinct request types i.e RegistrationForm and PaymentInformation all hell breaks loose.

Image for post

The validators which are bound using @initbinder will be applied by default to all endpoints of the controller irrespective of type.

Thus the validation will now be enforced on both RegistartionForm and PaymentInformation although our intention was to just validate the input of type RegistrationForm. This consequently leads to an IllegalStateException as spring now attempts to validate PaymentInformation request with the EmailValidator when a request is sent to the Payment endpoint

spring-boot validation spring-framework

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Spring Boot REST Validation Example | Spring Boot REST API Request Body Validation Example

Spring Boot REST Validation Example | Spring Boot REST API Request Body Validation Example

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

Learn Spring Framework and Spring Boot - Build a Spring Web Application

Learn Spring Framework and Spring Boot - Build a Spring Web Application: If you're new to the Spring Framework, this is the course you want to start with. This course covers the core of the Spring Framework, the foundation which all of the other Spring Framework projects are built from.

Spring Live | Introduction to Spring Framework | Java Spring Tutorial | Spring Training

Spring Live , Introduction to Spring Framework, Java Spring Tutorial ,Spring Training .Spring Framework Certification Training "Introduction to Spring Framework"

Spring vs Spring Boot

We will see how Spring and Spring Boot can be compared. Programmers need not start from scratch when they want to work on some projects.