Still Use If/else perform validation in Spring 6.0+ / SpringBoot 3.0+?

NGU
Javarevisited
Published in
10 min readFeb 17, 2024

--

If so pls upgrade with below best practices.

For web services, to avoid illegal parameters affecting the business, parameter testing must be done in the Controller layer! In most cases, the request parameters are divided into the following two forms:

  • POST, PUT requests, use requestBody to pass parameters
  • GET requests, use requestParam/PathVariable to pass parameters.

Prerequisite

  • Spring 6.0+
  • SpringBoot 3.0+

The Java API specification (JSR303) defines the standard validation-api for Bean, but does not provide an implementation. Hibernate validation is an implementation of this standard and has added validation annotations such as @Email, @Length, etc. Spring Validation is a secondary encapsulation of hibernate validation, used to support automatic validation of spring mvc parameters.

Next, let’s use the spring-boot project as an example to introduce the use of Spring Validation.

In SpringBoot 3.0+ .

the validation lib been moved into jakarta.validation

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

which could import by

<dependency>
<groupId>org.springframework.boot</groupId>…

--

--

NGU
Javarevisited

Microservices. Cloud Native. Architecture. Record Technology, Sports, Life. A picture is worth a thousand words