From e15c992a728f1a08f696d97c8b1092f5c1a92376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aljoscha=20Z=C3=B6ller?= Date: Mon, 3 Jun 2024 16:04:03 +0200 Subject: [PATCH] Add validation to NewRestaurantDTO and handle exceptions Added @NotBlank validation to the NewRestaurantDTO fields and included the spring-boot-starter-validation dependency in pom.xml. Created new classes ExceptionResponse and ExceptionResponseMessage to handle validation errors. The GlobalExceptionHandler class was also introduced to capture MethodArgumentNotValidException and provide appropriate response messages. --- backend/pom.xml | 6 ++-- .../exceptions/GlobalExceptionHandler.java | 28 +++++++++++++++++++ .../exceptions/domain/ExceptionResponse.java | 8 ++++++ .../domain/ExceptionResponseMessage.java | 7 +++++ .../restaurant/RestaurantController.java | 3 +- .../restaurant/domain/NewRestaurantDTO.java | 4 +++ 6 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/com/neuefische/team2/backend/exceptions/GlobalExceptionHandler.java create mode 100644 backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponse.java create mode 100644 backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponseMessage.java diff --git a/backend/pom.xml b/backend/pom.xml index 35e6bf6..0aaf2df 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -28,14 +28,16 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + de.flapdoodle.embed de.flapdoodle.embed.mongo.spring3x 4.13.0 test - - org.springframework.boot spring-boot-starter-test diff --git a/backend/src/main/java/com/neuefische/team2/backend/exceptions/GlobalExceptionHandler.java b/backend/src/main/java/com/neuefische/team2/backend/exceptions/GlobalExceptionHandler.java new file mode 100644 index 0000000..4555b38 --- /dev/null +++ b/backend/src/main/java/com/neuefische/team2/backend/exceptions/GlobalExceptionHandler.java @@ -0,0 +1,28 @@ +package com.neuefische.team2.backend.exceptions; + +import com.neuefische.team2.backend.exceptions.domain.ExceptionResponse; +import com.neuefische.team2.backend.exceptions.domain.ExceptionResponseMessage; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.List; + + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ExceptionResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) { + + List errors = exception.getFieldErrors() + .stream() + .map(error -> new ExceptionResponseMessage(error.getField(), error.getDefaultMessage())) + .toList(); + + return new ExceptionResponse(errors); + } +} diff --git a/backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponse.java b/backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponse.java new file mode 100644 index 0000000..3fc4a61 --- /dev/null +++ b/backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponse.java @@ -0,0 +1,8 @@ +package com.neuefische.team2.backend.exceptions.domain; + +import java.util.List; + +public record ExceptionResponse( + List errors +) { +} diff --git a/backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponseMessage.java b/backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponseMessage.java new file mode 100644 index 0000000..d08f120 --- /dev/null +++ b/backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponseMessage.java @@ -0,0 +1,7 @@ +package com.neuefische.team2.backend.exceptions.domain; + +public record ExceptionResponseMessage( + String field, + String message +) { +} diff --git a/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java b/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java index 61a5acb..750f732 100644 --- a/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java +++ b/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java @@ -2,6 +2,7 @@ import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO; import com.neuefische.team2.backend.restaurant.domain.Restaurant; +import jakarta.validation.Valid; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @@ -24,7 +25,7 @@ public List getRestaurants() { @PostMapping @ResponseStatus(HttpStatus.CREATED) - public Restaurant addRestaurant(@RequestBody NewRestaurantDTO newRestaurantDTO) { + public Restaurant addRestaurant(@RequestBody @Valid NewRestaurantDTO newRestaurantDTO) { Restaurant restaurant = new Restaurant(null, newRestaurantDTO.title(), newRestaurantDTO.city()); return restaurantService.addRestaurant(restaurant); } diff --git a/backend/src/main/java/com/neuefische/team2/backend/restaurant/domain/NewRestaurantDTO.java b/backend/src/main/java/com/neuefische/team2/backend/restaurant/domain/NewRestaurantDTO.java index 389ca90..323331b 100644 --- a/backend/src/main/java/com/neuefische/team2/backend/restaurant/domain/NewRestaurantDTO.java +++ b/backend/src/main/java/com/neuefische/team2/backend/restaurant/domain/NewRestaurantDTO.java @@ -1,7 +1,11 @@ package com.neuefische.team2.backend.restaurant.domain; +import jakarta.validation.constraints.NotBlank; + public record NewRestaurantDTO( + @NotBlank(message="Title must not be empty") String title, + @NotBlank(message="City must not be empty") String city ) { }