Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Last active April 28, 2020 02:39
Show Gist options
  • Save CheolhoJeon/3132e58049253337148c360830fdb069 to your computer and use it in GitHub Desktop.
Save CheolhoJeon/3132e58049253337148c360830fdb069 to your computer and use it in GitHub Desktop.
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