Hackforge Academy

Category: java

Spring Boot validation

Published on 06 Apr 2026

Explanation

Spring Boot field validation ensures incoming request data is correct before processing. It helps prevent invalid input such as empty fields, wrong email format, or short passwords.

Code:

// Add dependency (Maven)
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation
</artifactId>
</dependency>

Explanation

Validation annotations are added inside DTO classes to define rules like required fields, email format, or minimum length.

Code:

import jakarta.validation.constraints.*;

public class StudentDTO {

    @NotBlank(message = "Name is required")
    private String name;

    @Email(message = "Invalid email format")
    private String email;

    @Size(min = 6, message = "Password 
must be at least 6 characters")
    private String password;
}

Explanation

@Valid annotation is used in controller methods to trigger validation automatically when request data is received.

Code:

import org.springframework.web.
bind.annotation.*;
import jakarta.validation.Valid;

@RestController
@RequestMapping("/students")
public class StudentController {

    @PostMapping
    public String createStudent(
@Valid @RequestBody StudentDTO studentDTO) {
        return "Student created 
successfully";
    }
}

Explanation

Common validation annotations include @NotNull, @NotBlank, @Email, @Size, @Min, and @Max to enforce input constraints.

Code:

public class UserDTO {

   @NotNull(message = "ID cannot be null")
   private Long id;

    @NotBlank(message = "Username required")
    private String username;

    @Min(value = 18,
message = "Minimum age is 18")
    private int age;
}

Explanation

Validation errors can be captured using BindingResult to return custom error messages instead of default responses.

Code:

import org.springframework.
validation.BindingResult;

@PostMapping("/register")
public Object registerUser(@Valid 
@RequestBody 
UserDTO userDTO, BindingResult result) {

    if(result.hasErrors()) {
        return result.getAllErrors();
    }

    return "User registered successfully";
}

Explanation

Field validation improves API reliability and security by preventing invalid or incomplete data from entering backend systems such as student registration or certificate workflows in training platforms πŸŽ“.

Code:

@NotBlank(message = "Course name required")
private String courseName;

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community