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 handleMethodArgumentNotValid( + MethodArgumentNotValidException ex, + HttpHeaders headers, HttpStatusCode status, + WebRequest request) { + Map body = new LinkedHashMap<>(); + List errors = ex.getBindingResult().getAllErrors().stream() + .map(this::getErrorMessage) + .toList(); + body.put("errors", errors); + return new ResponseEntity<>(body,headers,status); + } + + @ExceptionHandler(EntityNotFoundException.class) + public ResponseEntity handleEntityNotFoundException( + EntityNotFoundException ex) { + System.out.println(ex.getMessage()); + return new ResponseEntity<>(ex.getLocalizedMessage(), HttpStatus.BAD_REQUEST); + } + + private String getErrorMessage(ObjectError e) { + if (e instanceof FieldError) { + String field = ((FieldError) e).getField(); + String message = e.getDefaultMessage(); + return String.format("%s %s",field,message); + } + return e.getDefaultMessage(); + } +} diff --git a/src/main/java/com/example/springbootbookshop/service/impl/BookServiceImpl.java b/src/main/java/com/example/springbootbookshop/service/impl/BookServiceImpl.java index d3abaec..b53282e 100644 --- a/src/main/java/com/example/springbootbookshop/service/impl/BookServiceImpl.java +++ b/src/main/java/com/example/springbootbookshop/service/impl/BookServiceImpl.java @@ -3,10 +3,10 @@ import com.example.springbootbookshop.dto.BookDto; import com.example.springbootbookshop.dto.CreateBookRequestDto; import com.example.springbootbookshop.entity.Book; +import com.example.springbootbookshop.exception.EntityNotFoundException; import com.example.springbootbookshop.mapper.BookMapper; import com.example.springbootbookshop.repository.BookRepository; import com.example.springbootbookshop.service.BookService; -import jakarta.persistence.EntityNotFoundException; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component;