Last active
April 28, 2020 02:39
-
-
Save CheolhoJeon/3132e58049253337148c360830fdb069 to your computer and use it in GitHub Desktop.
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
import org.springframework.web.bind.MethodArgumentNotValidException; | |
@RestControllerAdvice("controller") | |
public class ApiExceptionAdvice { | |
@ExceptionHandler(MethodArgumentNotValidException.class) | |
public ResponseEntity<ErrorResponse> handleBindException(MethodArgumentNotValidException ex) { | |
String errorCodes = ex | |
.getBindingResult() | |
.getAllErrors() | |
.stream() | |
.map(error -> error.getCodes()[0]) | |
.collect(Collectors.joining(",")); | |
return ResponseEntity | |
.status(HttpStatus.BAD_REQUEST) | |
.body(new ErrorResponse("errorCodes = " + errorCodes)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment