Springboot global exception handling method
GlobalException
import .slf4j.Slf4j; import ; import ; import ; import ; import ; import ; import ; import ; @Slf4j @ResponseBody @ControllerAdvice public class GlobalException { /** * System exception handling * * @param ex Exception * @return */ @ExceptionHandler(value = ) public Result defaultErrorHandler(Exception ex) { ("System exception:{}", ex); return (StatusEnum.SYSTEM_ERROR); } /** * BindException * * @param e Exception * @return */ @ExceptionHandler() public Result handleBingException(BindException e) { FieldError fieldError = (); String message = (); return (StatusEnum.SYSTEM_ERROR); } /** * Handle @RequestBody parameter verification exception * * @param e Exception information * @return */ @ExceptionHandler() public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { BindingResult bindingResult = (); FieldError fieldError = (); String code = (); String message = (); return (errorCode, message); } }
result
@Data public class Result<T> { /** * Status value */ private Boolean status; /** * Status code */ private String code; /** * Status information */ private String message; /** *Returned results */ private T result; /** * Constructor * * @param code status code * @param message prompt message */ public Result(String code, String message) { = false; = code; = message; } /** * Constructor * * @param status * @param code status code * @param message prompt message */ public Result(Boolean status, String code, String message) { = status; = code; = message; } /** * Constructor * * @param status * @param code status code * @param message prompt message * @param result */ public Result(Boolean status, String code, String message, T result) { = status; = code; = message; = result; } // ------------------------------------------------------------------------------------------------------------------------------ /** * Enumeration status code */ private static final String CODE; /** * Enumerated prompt information */ private static final String MESSAGE; /** * 500 */ private static final String ERROR_CODE; /** * The status code returned successfully */ private static final String SUCCESS_CODE; /** * Status information returned successfully */ private static final String SUCCESS_MSG; /** * Static code block initialization */ static { // The status code that was successfully returned CODE = "getCode"; // Enumerated prompt information MESSAGE = "getMessage"; //Exception code ERROR_CODE = "500"; // The status code that was successfully returned SUCCESS_CODE = "200"; // Status information returned successfully SUCCESS_MSG = "The operation is successful"; } /** * Operation is successful * * @return Return result */ public static Result success() { return new Result(true, SUCCESS_CODE, SUCCESS_MSG); } /** * Operation is successful * * @return Return result */ public static <T> Result success(T result) { return new Result(true, SUCCESS_CODE, SUCCESS_MSG, result); } /** * Returns exception * * @param t Generic Enumeration * @param <T> Generics * @return Return result */ public static <T extends Enum> Result error(T t) { return error(t, CODE, MESSAGE, null); } /** * Returns exception * * @param t Generic Enumeration * @param message prompt message * @param <T> Generics * @return Return result */ public static <T extends Enum> Result error(T t, String message) { return error(t, CODE, MESSAGE, message); } /** * Returns exception * * @param code status code * @return Return result */ public static Result error(String code) { return new Result(code, null); } /** * Returns exception * * @param code status code * @param message prompt message * @return Return result */ public static Result error(String code, String message) { return new Result(code, message); } /** * Returns exception * * @param t Returned enum * @param codeMethod state method * @param msgMethod status code for prompt message * @param <T> Generics * @return Return result object */ public static <T extends Enum> Result error(T t, String codeMethod, String msgMethod, String message) { try { Class<?> aClass = (); String code = (codeMethod).invoke(t).toString(); message = (message) ? (msgMethod).invoke(t).toString() : message; return new Result(code, message); } catch (Exception e) { return new Result(ERROR_CODE, ()); } } // --- Rewrite method --- /** * Rewrite the toString method * * @return Return JSON string */ @Override public String toString() { return (this); } /** * toJsonString method * * @return Return JSON string */ public String toJsonString() { return (this); } }
StatusEnum
public enum StatusEnum { /** * Operation is successful */ OK("200", "The operation is successful"), /** * No access permission */ NOT_PERMISSION("403", "No access permission"), /** * No login or login has expired */ NOT_LOGIN("405", "No login or login has expired"), /** * The system is busy, please try again later */ SYSTEM_ERROR("500", "The system is busy, please try again later"); /** * Constructor * * @param code status code * @param message prompt message */ StatusEnum(String code, String message) { = code; = message; } /** * Status code */ private String code; /** * Prompt message */ private String message; /** * Get the status code * * @return status code */ public String getCode() { return code; } /** * Get prompt information * * @return prompt message */ public String getMessage() { return message; } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.