S SmartDocs
系列: Springboot java 58 行 · 更新于 2026-08-14

BookResponse.java

Springboot/bookstore-api/src/main/java/com/example/bookstore/dto/BookResponse.java

package com.example.bookstore.dto;

import com.example.bookstore.entity.Book;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 「書籍」的回應 DTO——回傳給客戶端的 JSON 格式。
 *
 * 使用回應 DTO 的好處:
 *   1. 精準控制要對外公開的欄位(例如未來 Entity 加了內部欄位也不會外洩)。
 *   2. 與資料庫結構解耦,調整資料表不影響 API 契約。
 *
 * Jackson 會把這個 record 自動序列化成 JSON,例如:
 * {
 *   "id": 1,
 *   "title": "Clean Code",
 *   "author": "Robert C. Martin",
 *   "isbn": "9780132350884",
 *   "price": 1200.00,
 *   "stock": 10,
 *   "description": "...",
 *   "createdAt": "2026-08-14T10:30:00",
 *   "updatedAt": "2026-08-14T10:30:00"
 * }
 */
public record BookResponse(
        Long id,
        String title,
        String author,
        String isbn,
        BigDecimal price,
        Integer stock,
        String description,
        LocalDateTime createdAt,
        LocalDateTime updatedAt
) {

    /**
     * 【靜態工廠方法】把 Entity 轉換成回應 DTO。
     * 集中轉換邏輯,讓 Service 層程式碼保持簡潔:
     * BookResponse.from(book) 或 method reference:BookResponse::from
     */
    public static BookResponse from(Book book) {
        return new BookResponse(
                book.getId(),
                book.getTitle(),
                book.getAuthor(),
                book.getIsbn(),
                book.getPrice(),
                book.getStock(),
                book.getDescription(),
                book.getCreatedAt(),
                book.getUpdatedAt()
        );
    }
}

相关文章

Springboot java EN 更新于 2026-08-14

BookstoreApiApplication.java

BookstoreApiApplication.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/BookstoreApiApplication.java).

阅读文章 →
Springboot java EN 更新于 2026-08-14

DataInitializer.java

DataInitializer.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/config/DataInitializer.java).

阅读文章 →
Springboot java EN 更新于 2026-08-14

SecurityConfig.java

SecurityConfig.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/config/SecurityConfig.java).

阅读文章 →
Springboot java EN 更新于 2026-08-14

AuthController.java

AuthController.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/controller/AuthController.java).

阅读文章 →
Springboot java EN 更新于 2026-08-14

BookController.java

BookController.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/controller/BookController.java).

阅读文章 →
Springboot java EN 更新于 2026-08-14

ApiErrorResponse.java

ApiErrorResponse.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/dto/ApiErrorResponse.java).

阅读文章 →