Home  >  Article  >  Java  >  How to solve SpringBoot global exception problem

How to solve SpringBoot global exception problem

王林
王林forward
2023-05-16 10:49:121550browse

SpringBoot is a product born to simplify a series of issues such as the creation, running, debugging, and deployment of Spring applications. The feature of automatic assembly allows us to better focus on the business itself rather than external XML configuration. We As long as you follow the specifications and introduce relevant dependencies, you can easily build a WEB project

In actual project development, various abnormal situations often occur in the program, especially as a server As developers, we are always writing interfaces to provide for front-end calls. In the case of division of labor and cooperation, exceptions cannot be avoided. If the wrong information is directly exposed to the user, the experience can be imagined, and it will be harmful to the user. For hackers, detailed exception information is often of great help...

Adopt the try-catch method to manually capture the exception information and then return the corresponding result set. I believe many people have seen similar code. (For example: encapsulated into a Result object); although this method indirectly solves the problem of error exposure, the same disadvantages are also obvious, increasing the amount of code. When there are too many exceptions, the corresponding catch layers become more and more numerous. It is difficult to manage the matching between these business exceptions and error codes, so the best way is to control the global situation through simple configuration...

Let’s take a look at the solution provided by Spring Boot

1 Import dependencies

Add spring-boot-starter-web dependencies in pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Customize exceptions
During the application development process, except In addition to the exceptions of the system itself, the exceptions used in different business scenarios are also different. In order to be more relevant to the title of Easily Get Global Exceptions, define your own exceptions and see how to capture...

package com.battcn.exception;

/**
 * 自定义异常
 *
 * @author Levin
 * @since
public class CustomException extends RuntimeException

    private static final long serialVersionUID = 4564124491192825748L;

    private int code;

    public CustomException() {
        super();
    }

    public CustomException(int code, String message) {
        super(message);
        this.setCode(code);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}

2 Exception information Template

Define the format of the returned exception information, so that the style of the exception information is more unified

package com.battcn.exception;

/**
 * @author Levin
 * @since
public class ErrorResponseEntity

    private int code;
    private String message;
    // 省略 get set

3 Control layer

Take a closer look to see if it is the same as the code written normally. What's the difference, don't worry, keep reading...

package com.battcn.controller;

import com.battcn.exception.CustomException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常演示
 *
 * @author Levin
 * @since
@RestController
public class ExceptionController

    @GetMapping("/test3")
    public String test3(Integer num) {
        // TODO 演示需要,实际上参数是否为空通过 @RequestParam(required = true)  就可以控制
        if (num == null) {
            throw new CustomException(400, "num不能为空");
        }
        int i = 10 / num;
        return "result:"

4 Exception handling (key)

Annotation overview:

  • @ControllerAdviceCatch the exception thrown by the Controller layer. If @ResponseBody is added, the return information will be in JSON format.

  • @RestControllerAdviceEquivalent to the combination of @ControllerAdvice and @ResponseBody.

  • @ExceptionHandlerUniformly handles one type of exceptions to reduce code duplication and complexity.

Create a GlobalExceptionHandler class and add the @RestControllerAdvice annotation to define an exception notification class, and then add @ExceptionHandler to the defined method to realize exception capture...

package com.battcn.config;

import com.battcn.exception.CustomException;
import com.battcn.exception.ErrorResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理
 *
 * @author Levin
 * @since
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler


    /**
     * 定义要捕获的异常 可以多个 @ExceptionHandler({})
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return
    @ExceptionHandler(CustomException.class)
    public ErrorResponseEntity customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        CustomException exception = (CustomException) e;
        return new ErrorResponseEntity(exception.getCode(), exception.getMessage());
    }

    /**
     * 捕获  RuntimeException 异常
     * TODO  如果你觉得在一个 exceptionHandler 通过  if (e instanceof xxxException) 太麻烦
     * TODO  那么你还可以自己写多个不同的 exceptionHandler 处理不同异常
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return
    @ExceptionHandler(RuntimeException.class)
    public ErrorResponseEntity runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        RuntimeException exception = (RuntimeException) e;
        return new ErrorResponseEntity(400, exception.getMessage());
    }

    /**
     * 通用的接口映射异常处理方
     */
    @Override
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
                                                             HttpStatus status, WebRequest request) {
        if (ex instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
            return new ResponseEntity<>(new ErrorResponseEntity(status.value(), exception.getBindingResult().getAllErrors().get(0).getDefaultMessage()), status);
        }
        if (ex instanceof MethodArgumentTypeMismatchException) {
            MethodArgumentTypeMismatchException exception = (MethodArgumentTypeMismatchException) ex;
            logger.error("参数转换失败,方法:" + exception.getParameter().getMethod().getName() + ",参数:" + exception.getName()
                    + ",信息:" + exception.getLocalizedMessage());
            return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
        }
        return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
    }
}

Test

After completing the preparations, start Chapter17Application. From the following test results, you can find that it is really so easy, the code has become cleaner, and the scalability has also improved...

Visit http://localhost:8080/test3

{"code":400,"message":"num不能为空"}

Visit http://localhost:8080/test3?num=0

{"code":400,"message":"/ by zero"}

Visit http://localhost:8080 /test3?num=5

result:2

The above is the detailed content of How to solve SpringBoot global exception problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete