spring boot teaches you how to handle global exceptions

spring boot global exception handling

Handling global exceptions requires three steps

  • Custom exception enumeration
  • Custom exception
  • Sprboot AOP exception handling

Below I write a relatively simple, hope to combine according to their own situation, here I simply write some code ideas.

1 Custom Same Enumeration

public enum WebExceptionEnum {
    /**
     * SYS_ERROR      system file error
     * UNKNOWN_ERROR  Unknown System Error
     * SERVICE_INVOKE_ERROR   Server Call Error
     * ILLEGAL_ARGS   Input parameter incorrect
     */


    SYS_ERROR("SYS_ERROR", "System Error Please Retry"),
    UNKNOWN_ERROR("UNKNOWN_ERROR", "Unknown System Error"),
    SERVICE_INVOKE_ERROR("SERVICE_INVOKE_ERROR", "Server Call Error"),
    ILLEGAL_ARGS("ILLEGAL_ARGS", "Failure of parameter validation");


    private String exception;
    private String massage;

    WebExceptionEnum(String exception, String massage) {
        this.exception = exception;
        this.massage = massage;
    }

    public String getException() {
        return exception;
    }

    public String getMassage() {
        return massage;
    }


    public static WebExceptionEnum getWebException(String exception) {
        for (WebExceptionEnum results : WebExceptionEnum.values()) {
            if (results.getException().equals(exception)) {
                return results;
            }
        }
        return null;
    }
}

2 custom exception

public class MessageCenterException extends RuntimeException{

    private static final long serialVersionUID = -8581672033133636908L;

    //Exception state enumeration type
    private WebExceptionEnum exceptionEnum;

    //set method
    private void setExceptionEnum(WebExceptionEnum exceptionEnum){
        this.exceptionEnum = exceptionEnum;
    }

    //Parametric-free construction method
    public WebExceptionEnum getExceptionEnum(){
        return exceptionEnum;
    }

    //Get enumerated exception information
    public MessageCenterException(WebExceptionEnum exceptionEnum){
        super(exceptionEnum.getMassage());

        this.setExceptionEnum(exceptionEnum);
    }

    //Getting exception information
    @Override
    public String getMessage() {
        return exceptionEnum.getMassage();
    }
}

3 Global exception handling

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // Global error exception handling
    //

    /**
     * oracle  Stored procedure maintenance
     * Database layer exception
     * Business layer exception
     */
    //    value handles that exception
    @ExceptionHandler(MessageCenterException.class)
    public Result baseException(HttpServletRequest request, MessageCenterException ex) {
        log.error(ex.getMessage());
        return ErrorResult.error(ex.getMessage());
    }

}

Then return to the same in the project. Generally, an exception class is enough. If it is very complex, it can be extended.

Keywords: Spring Oracle Stored Procedure Database

Added by drepster on Wed, 02 Oct 2019 01:53:05 +0300