0% found this document useful (0 votes)
5 views2 pages

SpringBoot Annotations Guide

The document provides a comprehensive guide to Spring Boot annotations, detailing their purposes and providing examples for each. It covers validation annotations such as @Valid, @NotNull, @NotEmpty, @Size, @Email, @Min, and @Max, explaining how they enforce constraints on object fields. Additional sections address JPA, Security, AOP, Lifecycle, and Testing annotations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

SpringBoot Annotations Guide

The document provides a comprehensive guide to Spring Boot annotations, detailing their purposes and providing examples for each. It covers validation annotations such as @Valid, @NotNull, @NotEmpty, @Size, @Email, @Min, and @Max, explaining how they enforce constraints on object fields. Additional sections address JPA, Security, AOP, Lifecycle, and Testing annotations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Complete Guide to Spring Boot Annotations with Explanations and Examples

(...existing content...)

---

5. Validation Annotations (javax.validation / jakarta.validation)

@Valid

Purpose:

Used to trigger validation on an object passed as a parameter in a controller or service method.

Example:

@PostMapping("/users")

public ResponseEntity<?> createUser(@Valid @RequestBody User user) {

return ResponseEntity.ok(userService.save(user));

@NotNull, @NotEmpty, @NotBlank

Purpose:

Validate that fields are not null or empty:

- @NotNull Value must not be null.

- @NotEmpty Value must not be null and must not be empty (applies to strings, collections).

- @NotBlank Value must not be null, empty, or whitespace (applies to strings).

Example:

public class User {

@NotBlank(message = "Name is required")


private String name;

@Size

Purpose:

Restricts the size (length for Strings, size for collections).

Example:

@Size(min = 8, message = "Password must be at least 8 characters")

private String password;

@Email

Purpose:

Ensures the field has a valid email format.

Example:

@Email(message = "Invalid email")

private String email;

@Min and @Max

Purpose:

Restrict a numeric value within minimum and maximum bounds.

Example:

@Min(18)

@Max(60)

private int age;

... (and so on for JPA, Security, AOP, Lifecycle, and Testing annotations)

You might also like