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); } }</object>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.