Xây dựng REST API hoàn chỉnh với Spring Boot 3
Spring Boot 3 chạy trên Java 17+, hỗ trợ virtual threads, GraalVM native image và chuẩn hoá xử lý lỗi theo RFC 7807.
Khởi tạo dự án
curl https://start.spring.io/starter.zip \
-d dependencies=web,data-jpa,validation,postgresql \
-d javaVersion=21 -d type=gradle-project \
-d artifactId=bookstore -o bookstore.zipXử lý lỗi chuẩn RFC 7807 với ProblemDetail
@RestControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler(BookNotFoundException.class)
ProblemDetail handleNotFound(BookNotFoundException ex) {
var pd = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
pd.setTitle("Book not found");
return pd;
}
}Mọt Dev
Backend Engineer — Java, Spring Boot, Apache Kafka, Oracle SQL & System Design.
Bình luận (0)
Bạn cần đăng nhập bằng Google để bình luận.
Hãy là người bình luận đầu tiên.


