diff --git a/pom.xml b/pom.xml
index f5a5d2f..b00fc6b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,6 +20,11 @@
1.5.5.Final
+
+ org.springframework.boot
+ spring-boot-starter-validation
+ 3.1.4
+
com.h2database
h2
diff --git a/src/main/java/com/example/springbootbookshop/controller/BookController.java b/src/main/java/com/example/springbootbookshop/controller/BookController.java
index e188882..cb30a47 100644
--- a/src/main/java/com/example/springbootbookshop/controller/BookController.java
+++ b/src/main/java/com/example/springbootbookshop/controller/BookController.java
@@ -4,6 +4,7 @@
import com.example.springbootbookshop.dto.CreateBookRequestDto;
import com.example.springbootbookshop.entity.Book;
import com.example.springbootbookshop.service.BookService;
+import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
@@ -43,7 +44,7 @@ public BookDto getBookById(@PathVariable Long id) {
@PutMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK)
- public BookDto getBookById(@RequestBody CreateBookRequestDto bookDto,
+ public BookDto updateBookById(@RequestBody @Valid CreateBookRequestDto bookDto,
@PathVariable Long id) {
return bookService.update(bookDto, id);
}
diff --git a/src/main/java/com/example/springbootbookshop/dto/CreateBookRequestDto.java b/src/main/java/com/example/springbootbookshop/dto/CreateBookRequestDto.java
index 7efc908..2a2267a 100644
--- a/src/main/java/com/example/springbootbookshop/dto/CreateBookRequestDto.java
+++ b/src/main/java/com/example/springbootbookshop/dto/CreateBookRequestDto.java
@@ -1,11 +1,14 @@
package com.example.springbootbookshop.dto;
+import jakarta.validation.constraints.NotBlank;
+import jakarta.validation.constraints.NotNull;
+import jakarta.validation.constraints.Positive;
import java.math.BigDecimal;
-public record CreateBookRequestDto(String title,
- String author,
- String isbn,
- BigDecimal price,
+public record CreateBookRequestDto(@NotBlank String title,
+ @NotBlank String author,
+ @NotBlank String isbn,
+ @NotNull @Positive BigDecimal price,
String description,
String coverImage) {
}
diff --git a/src/main/java/com/example/springbootbookshop/exception/CustomGlobalExceptionHandler.java b/src/main/java/com/example/springbootbookshop/exception/CustomGlobalExceptionHandler.java
new file mode 100644
index 0000000..ee4eaf5
--- /dev/null
+++ b/src/main/java/com/example/springbootbookshop/exception/CustomGlobalExceptionHandler.java
@@ -0,0 +1,48 @@
+package com.example.springbootbookshop.exception;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.HttpStatusCode;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.FieldError;
+import org.springframework.validation.ObjectError;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+@ControllerAdvice
+public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler {
+ @Override
+ protected ResponseEntity