BookPatchRequest.java
Springboot/bookstore-api/src/main/java/com/example/bookstore/dto/BookPatchRequest.java
package com.example.bookstore.dto;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
/**
* 「部分更新(PATCH)書籍」的請求 DTO。
*
* 【PATCH 與 PUT 的關鍵差異】
* PUT = 完整替換:所有欄位都必填,沒帶到的欄位視同要清空 → 用 BookRequest
* PATCH = 部分更新:只更新「有帶值」的欄位,其餘維持原狀 → 用本類別
*
* 因此本 DTO 的所有欄位都「允許為 null」(沒有 @NotBlank / @NotNull)。
* Service 層會逐一判斷:欄位不是 null 才更新對應的資料。
*
* 【小知識】@Size、@Pattern、@DecimalMin 這些註解遇到 null 值會「直接跳過」
* (Bean Validation 規範:null 交給 @NotNull 把關),
* 所以它們只會驗證「有傳入」的欄位,正好符合 PATCH 的語意。
*/
public record BookPatchRequest(
@Size(max = 200, message = "書名長度不可超過 200 字")
String title,
@Size(max = 100, message = "作者長度不可超過 100 字")
String author,
@Pattern(regexp = "^[0-9\\-]{10,17}$",
message = "ISBN 格式不正確(10~17 位的數字與連字號)")
String isbn,
@DecimalMin(value = "0.0", message = "價格不可為負數")
BigDecimal price,
@Min(value = 0, message = "庫存不可為負數")
Integer stock,
@Size(max = 2000, message = "簡介長度不可超過 2000 字")
String description
) {
}
Articles liés
BookstoreApiApplication.java
BookstoreApiApplication.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/BookstoreApiApplication.java).
Lire l'article →DataInitializer.java
DataInitializer.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/config/DataInitializer.java).
Lire l'article →SecurityConfig.java
SecurityConfig.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/config/SecurityConfig.java).
Lire l'article →AuthController.java
AuthController.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/controller/AuthController.java).
Lire l'article →BookController.java
BookController.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/controller/BookController.java).
Lire l'article →ApiErrorResponse.java
ApiErrorResponse.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/dto/ApiErrorResponse.java).
Lire l'article →