Springboot custom global exception
Custom global exceptions
Encapsulated exception class under the bizException basic package
-
BizException
: Exceptions occurring in the runtime business
/** * @Description: Exception that occurs in the runtime business * @Param: * @Return : * @Author : l-jiahui * @Date: 2020-10-11 */ public class BizException extends RuntimeException { private static final long serialVersionUID = -7864604160297181941L; private final String code; /** * @Description : Specify the error class in the enumeration class * @Param : [errorCode] * @Return : * @Author : l-jiahui * @Date: 2020-10-11 */ public BizException(final BizExceptionCode exceptionCode) { super(()); = (); } /** * @Description: Specify the information about the specific business error * @Param : [detailedMessage] * @Return : * @Author : l-jiahui * @Date: 2020-10-11 */ public BizException(final String message) { super(message); = (); } public String getCode() { return code; } }
-
BizExceptionCode
: Error code interface for business exceptions
/** * @Description : Error code interface for business exceptions * @Param: * @Return : * @Author : l-jiahui * @Date: 2020-10-11 */ public interface BizExceptionCode { /** * @Description : Get the error code * @Param : [] * @Return : * @Author : l-jiahui * @Date: 2020-10-11 */ String getCode(); /** * @Description : Get error message * @Param : [] * @Return : * @Author : l-jiahui * @Date: 2020-10-11 */ String getMessage(); }
-
BizExceptionCodeEnum
: The enumeration class of exception messages (this class belongs to the business exception enumeration class)
/** * @Description : The enumeration class of exception message (this class belongs to the business exception enumeration class) * @Param: * @Return : * @Author : l-jiahui * @Date: 2020-10-11 */ public enum BizExceptionCodeEnum implements BizExceptionCode{ // The specified exception, when the exception is used, the message does not return to the front end, and the message specified when the front end is thrown. SPECIFIED("-1","The system exception occurs, please try again later"), // Common business exceptions USER_NAME_NULL("-1","The username cannot be empty, please re-enter!"), USER_PASSWORD_NULL("-1","The password cannot be empty, please re-enter!"), USER_PASSWORD_WRONG("-1","The password is wrong, please check and re-enter!"), PAGE_NUM_NULL("4001","The page number cannot be empty"), PAGE_SIZE_NULL("4002","The number of pages cannot be empty"), SEARCH_NULL("4004","The search criteria cannot be empty, please check and re-enter!"), NO_LOGIN("3001", "User not logged in") ; private final String code; private final String message; /** * @Description : * @Param : [code, message] * @Return : * @Author : l-jiahui * @Date : 2020-10-11 */ BizExceptionCodeEnum(String code,String message){ = code; = message; } @Override public String getCode() { return code; } @Override public String getMessage() { return message; } }
Global capture exceptions and custom global capture exceptions and 404 processing
/** * @author l-jiahui * extend of {@link ResponseEntityExceptionHandler} for handle all exception * @Description: Global catch exception and custom global catch exception */ @ControllerAdvice public class GlobalControllerAdvice { /** * Intercept and catch custom exceptions * * @param bizException Custom exception * @return map */ @ResponseBody @ExceptionHandler(value = ) public Map<String, Object> myExceptionHandler(BizException bizException, HttpServletResponse response) { Map<String, Object> map = new HashMap<>(16); ("code", ()); ("msg", ()); ((())); return map; } /** * Added unified error message for processing 404 * * @param response response object * @return Return the object information corresponding to the map */ @ExceptionHandler(value = {}) @ResponseStatus(HttpStatus.NOT_FOUND) @ResponseBody public Map<String, Object> notFoundException(HttpServletResponse response) { Map<String, Object> map = new HashMap<>(16); ("code", "404"); ("msg", "not found exception"); (404); return map; } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.