-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8081fe2
commit 096dd29
Showing
2 changed files
with
30 additions
and
2 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
27 changes: 27 additions & 0 deletions
27
src/main/java/guru/springframework/msscbeerservice/web/controller/MvcExceptionHandler.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,27 @@ | ||
package guru.springframework.msscbeerservice.web.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import javax.validation.ConstraintViolationException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by jt on 2019-05-25. | ||
*/ | ||
@ControllerAdvice | ||
public class MvcExceptionHandler { | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
public ResponseEntity<List> validationErrorHandler(ConstraintViolationException ex){ | ||
List<String> errorsList = new ArrayList<>(ex.getConstraintViolations().size()); | ||
|
||
ex.getConstraintViolations().forEach(error -> errorsList.add(error.toString())); | ||
|
||
return new ResponseEntity<>(errorsList, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
} |