Get the App
SLTechnology News&Howtos  ›  Development  › 

How to handle exceptions uniformly with SpringBoot

Shulou Source: shulou.com Published: 2022-06-01 19:32:35 10月04日 Update

This article mainly introduces the relevant knowledge of "how to unify exception handling in SpringBoot". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to unify exception handling in SpringBoot" can help you solve the problem.

1. Before processing

Exception code

/ * * obtain hospital settings according to id * @ param id id number * @ return * / @ ApiOperation (value = "obtain hospital settings according to id") @ GetMapping ("/ findHospById/ {id}") public Result findHospById (@ PathVariable Long id) {/ / simulate exception (because the divisor cannot be 0) int a = 1 / 0; HospitalSet hospitalSet = hospitalSetService.getById (id) Return Result.ok (hospitalSet);}

Swagger2 output result

2. Global handling of system exceptions

Add a global exception handling class

Code

Package com.fafa.yygh.common.exception;import com.fafa.yygh.common.result.Result;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody / * @ author Sire * @ version 1.0 * @ date 2022-02-02 21:01 * / @ ControllerAdvicepublic class GlobalExceptionHandler {/ * * system exception handling * * @ param e * @ return * / @ ExceptionHandler (Exception.class) @ ResponseBody public Result error (Exception e) {e.printStackTrace (); return Result.fail ();}}

Swagger2 result

3. Custom exception handling

When developing, we often need to define and handle some exceptions (here or the above exception for testing)

Create a custom exception handling class

Package com.fafa.yygh.common.exception;import com.fafa.yygh.common.result.ResultCodeEnum;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import lombok.Data;/** * Custom Global exception Class * * @ author qy * / @ Data@ApiModel (value = "Custom Global exception Class") public class YyghException extends RuntimeException {@ ApiModelProperty (value = "exception status Code") private Integer code / * * create exception object through status code and error message * * @ param message * @ param code * / public YyghException (String message, Integer code) {super (message); this.code = code } / * receive enumerated type objects * * @ param resultCodeEnum * / public YyghException (ResultCodeEnum resultCodeEnum) {super (resultCodeEnum.getMessage ()); this.code = resultCodeEnum.getCode () @ Override public String toString () {return "YyghException {" + "code=" + code + ", message=" + this.getMessage () +'}';}}

Add it to GlobalExceptionHandler

/ * * custom exception handling * * @ param e * @ return * / @ ExceptionHandler (YyghException.class) @ ResponseBodypublic Result divError (YyghException e) {return Result.build (e.getCode (), e.getMessage ());}

Need to manually try catch

Effect.

Swagger is the same as system exception handling.

But the background output is different.

This is the end of the introduction to "how to unify exception handling in SpringBoot". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Processing global unified knowledge system code hospital object status result industry output different practical content background practical manual effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft vpn macOS Huawei