In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.