Configure the global exception interceptor in springboot and return the self-determined HTTP error code to the front end


Create interceptor class and annotate @ controlleradvice annotation

public class GlobalExceptionAdvice {

    //Handle unknown exception
    public UnifyResponse handleException(HttpServletRequest request, Exception e){
        String method = request.getMethod();
        String requestUrl = request.getRequestURI();
        Unifyresponse unifyresponse = new unifyresponse (9999, "server exception", requesturl + "" + method);
        return unifyResponse;
public class UnifyResponse {

    private Integer code;
    private String message;
    private String request;

    public UnifyResponse(Integer code, String message, String request) {
        this.code = code;
        this.message = message;
        this.request = request;

    public UnifyResponse(){


    public String toString() {
        return "UnifyResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", request='" + request + '\'' +

     *An exception is thrown to indicate that the request is successful
    public static void crreateSuccess(){
        throw new CreateSuccess(1);

Create an exception handling method in the class

  • @Exceptionhandler: indicates which exceptions the method will handle.
  • @Responsestats: Specifies the error code returned from the front end.

The request and exception information can be obtained through the request and exception parameters, and the error information returned to the front end can be determined.
The above methods can basically meet most requirements, but it is still inconvenient to flexibly adjust the HTTP error codes returned to the front end. Different processing methods need to be established for each HTTP error code.

The implementation determines the HTTP error code of the message returned to the front end

public class HttpException extends RuntimeException{

    protected Integer code;
    protected Integer httpStatusCode = 500;

    public Integer getCode() {
        return code;

    public Integer getHttpStatusCode() {
        return httpStatusCode;
public class NotFoundException extends HttpException{

    public NotFoundException(int code){
        this.code = code;
        this.httpStatusCode = 404;

I create an HTTP exception base class to inherit runtimeException, then create different exception classes to implement httpexception according to different HTTP error codes, and pass in my own error code. If you do not pass in an error code, 500 is used by default

    public ResponseEntity handleHttpException(HttpServletRequest request, HttpException e){
        String method = request.getMethod();
        String requestUrl = request.getRequestURI();
        UnifyResponse unifyResponse = new UnifyResponse(e.getCode(), requestUrl + " " + method);
        HttpHeaders headers = new HttpHeaders();
        HttpStatus httpStatus = HttpStatus.resolve(e.getHttpStatusCode());
        ResponseEntity<UnifyResponse> responseEntity = new ResponseEntity(unifyResponse, headers, httpStatus);
        return responseEntity;

Create a new method to handle custom exceptions. In order to change the error code of the returned message, you need to set the HTTP header and create a responseentity. Pass in the HTTP header and HTTP error code object and return.