-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
53 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/neuefische/team2/backend/exceptions/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
...rc/main/java/com/neuefische/team2/backend/exceptions/domain/ExceptionResponseMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/com/neuefische/team2/backend/restaurant/domain/NewRestaurantDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |