In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.