Command Palette

Search for a command to run...

Bài viết với tag "spring boot"

[Spring Boot Basics] Server-side rendering với Thymeleaf trong Spring Boot: template, form và validation

Server-side rendering với Thymeleaf 3.1.5 trong Spring Boot 4.1.1, kiểm chứng trên application đang chạy: khi nào SSR hợp hơn JSON API, view name trở thành classpath:/templates/products/list.html ra sao, năm loại standard expression, th:text và th:utext với XSS, th:each cùng #numbers và #temporals, form tạo mới có validation với th:field và th:errors, vị trí bắt buộc của BindingResult, Post/Redirect/Get với flash attribute, fragment, CSS tĩnh, spring.thymeleaf.cache thực sự thay đổi gì, và trang lỗi HTML.

[Spring Boot Basics] Gọi API bên ngoài với RestClient trong Spring Boot: GET, POST, xử lý lỗi và timeout

Gọi HTTP API bên ngoài từ Spring Boot 4.1.1 bằng RestClient, kiểm chứng với một stub chạy local: RestClient so với RestTemplate, WebClient và @HttpExchange, spring-boot-starter-restclient và RestClient.Builder được auto-configure, GET vào record và list, toEntity, encode query parameter, POST, PUT và DELETE, message thật của HttpClientErrorException, onStatus, defaultStatusHandler và exchange, connect timeout và read timeout mặc định lẫn khi cấu hình bằng spring.http.clients được đo thực tế, interceptor để log, và chuyển lỗi upstream thành 502, 503 và 504.

[Spring Boot Basics] Tài liệu API trong Spring Boot với springdoc-openapi và Swagger UI

springdoc-openapi 3.1.1 trên Spring Boot 4.1.1, kiểm chứng trên jar đang chạy: document OpenAPI 3.1 ở /v3/api-docs, Swagger UI và Try it out, những gì springdoc suy ra từ controller, DTO record và Bean Validation constraint, response nào của @RestControllerAdvice được thêm vào, @Tag, @Operation, @ApiResponse, @Parameter và @Schema trên record, bean OpenAPI và customizer toàn cục, GroupedOpenApi, property của springdoc và tắt tài liệu trong profile prod.

[Spring Boot Basics] Kiến trúc phân lớp trong Spring Boot: Controller – Service – Repository và tổ chức package theo layer hay feature

Kiến trúc phân lớp trong Spring Boot 4.1.1, refactor và kiểm chứng trên một catalogue API thật: controller làm tất cả mọi việc và lỗi crash lúc khởi động khi tái sử dụng nó, controller, service và repository mỗi layer chịu trách nhiệm gì, map DTO ở layer nào, refactor từng bước và dùng curl chứng minh hành vi không đổi, service nên là interface hay class cụ thể kiểm tra bằng Mockito 5.23, năm anti-pattern khi phân layer, và so sánh tổ chức package theo layer với theo feature trên cùng một thay đổi, kèm bean package-private vẫn được inject và lỗi compile giữ các feature tách biệt.

[Spring Boot Basics] Xử lý exception tập trung trong Spring Boot: @RestControllerAdvice, @ExceptionHandler và ProblemDetail

Xử lý exception tập trung trong Spring Boot 4.1.1, kiểm chứng trên project thật: body /error mặc định và BasicErrorController, spring.web.error.* thay cho server.error.*, @ResponseStatus và ResponseStatusException, @ExceptionHandler trong controller và trong @RestControllerAdvice, cách Spring chọn một handler theo khoảng cách type, controller, @Order và cause, ProblemDetail (RFC 9457) với application/problem+json, ErrorResponseException, spring.mvc.problemdetails.enabled, ResponseEntityExceptionHandler trả 422 kèm danh sách lỗi theo field, và một handler catch-all giữ nguyên các response 4xx của framework.

[Spring Boot Basics] Validation trong Spring Boot: Bean Validation, @Valid và custom validator

Bean Validation trong Spring Boot 4.1.1 với Hibernate Validator 9.1.3, kiểm chứng bằng các lần chạy thật: spring-boot-starter-validation, @NotNull, @NotEmpty và @NotBlank khác nhau ra sao, @Size, @DecimalMin, @Digits, @Email và @Pattern trên DTO record, @Valid với @RequestBody và response 400 mặc định, object lồng nhau và list, validate @PathVariable và @RequestParam cùng cái bẫy 500 của @Validated, validation group, ValidationMessages.properties và Accept-Language, custom ConstraintValidator và constraint liên quan nhiều field, và validation ở service layer.