การตรวจสอบบีนด้วย @Valid
ตรวจสอบเนื้อหาคำขอด้วยคำกำกับ
การตรวจสอบบีนด้วย @Valid เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Bean Validation Overview
Bean Validation (JSR-380, Jakarta Validation) lets you declare constraints with annotations on your DTO fields, then validate objects against them. Spring Boot integrates it so incoming request bodies are checked automatically.
Adding the Dependency
The validation API plus an implementation (Hibernate Validator) come via the validation starter. Adding it activates annotation-driven validation in controllers.
<!-- pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>Constraints on a DTO
Annotate fields with constraints. Each describes a rule the value must satisfy, plus an optional message shown when it fails.
public class CreateUserRequest {
@NotBlank
private String name;
@Email @NotNull
private String email;
@Size(min = 8, max = 64)
private String password;
@Min(18)
private int age;
// getters/setters
}Common Constraints
The standard annotations cover most needs:
@NotNull— value must not be null@NotBlank— string must contain non-whitespace@NotEmpty— collection/string not empty@Size,@Min,@Max,@Email,@Pattern
Triggering Validation with @Valid
In a controller, annotate the request body parameter with @Valid. Spring validates it before your method body runs; on failure it short-circuits with an error.
@PostMapping("/users")
public ResponseEntity<Void> create(@Valid @RequestBody CreateUserRequest req) {
userService.create(req);
return ResponseEntity.status(HttpStatus.CREATED).build();
}What Happens on Failure
If validation fails, Spring throws MethodArgumentNotValidException for a @RequestBody. By default this yields a 400 Bad Request, so invalid input never reaches your service layer.
Custom Messages
Override the default message per constraint with the message attribute, or externalize messages for i18n via a resource bundle.
@Size(min = 8, message = "Password must be at least 8 characters")
private String password;Validating Path and Query Params
To validate individual method parameters (not a DTO), put constraints directly on them and add @Validated at the class level so method-level validation is active.
@Validated
@RestController
public class ProductController {
@GetMapping("/products")
public List<Product> list(@RequestParam @Min(1) int page) {
return service.page(page);
}
}Nested Validation
Validation does not automatically descend into nested objects. Mark the nested field with @Valid so its constraints are also checked.
public class OrderRequest {
@NotNull
@Valid
private AddressDto shippingAddress;
// ...
}Validating Collections
To validate each element of a collection, place @Valid on the collection field; each item’s constraints are then evaluated.
public class CartRequest {
@NotEmpty
private List<@Valid LineItem> items;
// ...
}Validation Groups
Validation groups let the same DTO enforce different rules in different contexts (for example create vs update). Annotate constraints with a group interface and validate against that group.
public interface OnCreate {}
public class UserDto {
@Null(groups = OnCreate.class)
private Long id; // must be null when creating
}Quick Check
Test your understanding of @Valid.
Recap
Bean Validation guards your input.
- Add
spring-boot-starter-validation - Declare constraints like
@NotBlank,@Size,@Emailon DTOs - Trigger with
@Validon request bodies; failures yield 400 - Use
@Validatedfor param-level validation - Nested objects and collections need their own
@Valid
คำถามที่พบบ่อย
บทเรียน “การตรวจสอบบีนด้วย @Valid” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจสอบบีนด้วย @Valid” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบบีนด้วย @Valid”
ตรวจสอบเนื้อหาคำขอด้วยคำกำกับ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจสอบบีนด้วย @Valid” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตรวจสอบบีนด้วย @Valid
- ตัวตรวจสอบแบบกำหนดเอง
- @ControllerAdvice และ @ExceptionHandler
- รายละเอียดปัญหา (RFC 7807)