Skip to content

Commit

Permalink
Add validation to NewRestaurantDTO and handle exceptions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
josch87 committed Jun 3, 2024
1 parent f91744a commit e15c992
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
6 changes: 4 additions & 2 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo.spring3x</artifactId>
<version>4.13.0</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ExceptionResponseMessage> errors = exception.getFieldErrors()
.stream()
.map(error -> new ExceptionResponseMessage(error.getField(), error.getDefaultMessage()))
.toList();

return new ExceptionResponse(errors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.neuefische.team2.backend.exceptions.domain;

import java.util.List;

public record ExceptionResponse(
List<ExceptionResponseMessage> errors
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.neuefische.team2.backend.exceptions.domain;

public record ExceptionResponseMessage(
String field,
String message
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -24,7 +25,7 @@ public List<Restaurant> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
) {
}

0 comments on commit e15c992

Please sign in to comment.