Implementation of ResponseResult in global exception handling throwing (optimizing business layer code)

In the development process, in addition to the exception of the system itself, the exceptions used in different business scenarios are different. In order to optimize the code, the business scenario only returns a desired result, and all other results use exception throwing. In this way, an exception handling class needs to be added to the controller layer, which implements the method of processing the exception thrown by the controller layer and returning the corresponding ResponseResult.

Writing a global exception handling class requires two very important annotations @RestControllerAdvice. @ExceptionHandler
@ControllerAdvice captures exceptions thrown by the Controller layer. If you add @ResponseBody, the return information will be in JSON format.
@RestControllerAdvice is equivalent to the combination of @ControllerAdvice and @ResponseBody.
@ExceptionHandler handles one type of exception uniformly, reducing code repetition rate and complexity.
Create a GlobalExceptionHandler class, and add @RestControllerAdvice annotation to define the exception notification class, and then add @ExceptionHandler to the defined method to achieve exception capture...
1. First, write a code that encapsulates the ResponseCode status code The global exception class inherits RuntimeException, adding a ResponseCode status code class variable, and passing the status code through it.

import com.example.result.ResponseCode;
import lombok.Data;
/**
 * 定义一个全局异常,继承RuntimeException,这样就不需要捕获可以直接抛出
 * 与Exception需要就在于这个需要捕获......
 * */
@Data
public class GlobalException extends RuntimeException{
    
    

    private ResponseCode rc;

    public GlobalException(ResponseCode rc){
    
    
        super(rc.toString());
        this.rc = rc;
    }
}

2. Write the GlobalExceptionHandler class, use the above two annotations, receive controller exceptions, and make corresponding exception results

import com.example.result.ResponseCode;
import com.example.result.ResponseResult;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**ControllerAdvice
 * ResponseBody
 * */
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    

    @ExceptionHandler(value = Exception.class)
    public ResponseResult<String> exceptionHandler(HttpServletRequest request, Exception e){
    
    
        if (e instanceof GlobalException){
    
    
            GlobalException ex = (GlobalException)e;
            return ResponseResult.error(ex.getRc());
        }else {
    
    
            return ResponseResult.error(ResponseCode.SERVER_ERROR);
        }
    }
}

Written in this way, the business layer service only needs to return the correct and desired result to the controller, and all others are thrown to the controller in an abnormal manner, and then accepted by the @ControllerAdvice annotation, and the corresponding exception is determined according to @ExceptionHandler and the result is thrown To the front end.

Guess you like

Origin blog.csdn.net/Wangdiankun/article/details/106175590