Unified Interface return and Global exception handling in SpringBoot
Global exception handling
Write a custom exception base class @ Datapublic class BaseException extends RuntimeException {private static final int BASE_EXCEPTION_CODE = ResultCode.INTERNAL_ERROR.code (); private static final String BASE_EXCEPTION_MESSAGE = ResultCode.INTERNAL_ERROR.message (); private Integer code; private String message; public BaseException () {super (BASE_EXCEPTION_MESSAGE); this.code = BASE_EXCEPTION_CODE; this.message = BASE_EXCEPTION_MESSAGE } public BaseException (String message) {super (message); this.code = BASE_EXCEPTION_CODE; this.message = message;} public BaseException (ResultCode resultCode) {super (resultCode.message ()); this.code = resultCode.code (); this.message = resultCode.message ();} public BaseException (Throwable cause) {super (cause) This.code = BASE_EXCEPTION_CODE; this.message = BASE_EXCEPTION_MESSAGE;} public BaseException (String message, Throwable cause) {super (message, cause); this.code = BASE_EXCEPTION_CODE; this.message = message;} public BaseException (Integer code, String message) {super (message); this.code = code; this.message = message } public BaseException (Integer code, String message, Throwable cause) {super (message, cause); this.code = code; this.message = message;}} write a custom business exception class public class BizException extends BaseException {public BizException (ResultCode resultCode) {super (resultCode);}} define a global exception handling class
Uniformly handle a certain type of exception through the @ ExceptionHandler annotation
@ Slf4j@RestControllerAdvicepublic class GlobalExceptionHandler {/ * uniformly handle custom base exceptions * / @ ExceptionHandler (BaseException.class) public ErrorResult baseException (BaseException e) {if (StringUtils.isEmpty (e.getCode ()) {return ErrorResult.error (e.getMessage ());} return ErrorResult.error (e.getCode (), e.getMessage () Unified handling of custom business exceptions * / @ ExceptionHandler (BizException.class) public ErrorResult bizException (BizException e) {if (StringUtils.isEmpty (e.getCode () {return ErrorResult.error (e.getMessage ());} return ErrorResult.error (e.getCode (), e.getMessage ()) } / * handle all exceptions except for custom exceptions * / @ ExceptionHandler (Exception.class) public ErrorResult handleException (Exception e) {log.error (e.getMessage (), e); return ErrorResult.error (e.getMessage ()) } / * compatible Validation verification framework: ignore parameter exception handler * / @ ExceptionHandler (MissingServletRequestParameterException.class) public ApiResult parameterMissingExceptionHandler (MissingServletRequestParameterException e) {log.error (e.getMessage (), e); return ErrorResult.error (PARAMETER_EXCEPTION, "request parameters" + e.getParameterName () + "cannot be empty") } / * compatible Validation check framework: missing request body exception handler * / @ ExceptionHandler (HttpMessageNotReadableException.class) public ErrorResult parameterBodyMissingExceptionHandler (HttpMessageNotReadableException e) {log.error (e.getMessage (), e); return ErrorResult.error (PARAMETER_EXCEPTION, "parameter body cannot be empty") } / * compatible Validation verification framework: parameter validation exception handler * / @ ExceptionHandler (MethodArgumentNotValidException.class) public ErrorResult parameterExceptionHandler (MethodArgumentNotValidException e) {log.error (e.getMessage (), e); / / get exception information BindingResult exceptions = e.getBindingResult () / / determine whether there is an error message in the exception, and if so, use the message in the exception, otherwise use the default message if (exceptions.hasErrors ()) {List errors = exceptions.getAllErrors () If (! errors.isEmpty ()) {/ / all the error parameters are listed here. According to normal logic, only the first error is needed to FieldError fieldError = (FieldError) errors.get (0); return ErrorResult.error (PARAMETER_EXCEPTION, fieldError.getDefaultMessage ()) }} return ErrorResult.error (PARAMETER_EXCEPTION, "request parameter verification exception");} API call @ ResponseResult @ GetMapping public User update () {/ / non-custom runtime exception long id = 10 / 0; return userService.queryById (id) } @ ResponseResult @ PostMapping public User insert () {/ / throw custom base exception throw new BaseException ();} @ ResponseResult @ DeleteMapping public boolean delete () {/ / throw custom business exception throw new BizException (USER_NOT_EXIST_ERROR);} test result
This is the end of the content of "unified interface return and global exception handling in SpringBoot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!