In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how springboot custom exceptions and catch exceptions back to the front end, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Background
In development, if you use try catch, each method needs to be implemented separately. In order to facilitate the classification of exceptions, it is returned to the front end, using the @ ControllerAdvice annotation and inheriting RuntimeException to implement.
Realized content
Three types of exceptions were caught
1. Business exception
BusinessException
two。 System anomaly
SystemException
3. Other anomalies
Use @ ExceptionHandler (RuntimeException.class) to capture
The ExceptionAdvice class catches the above three types of exceptions and returns custom type format data
Implementation code
Business exception BusinessException class implementation, inheriting RuntimeException
Public class BusinessException extends RuntimeException {/ * error coding * / private String code; public BusinessException () {super ();} public BusinessException (String message) {super (message);} public BusinessException (String code, String message) {super (message) This.code = code;} public BusinessException (Throwable cause) {super (cause);} public BusinessException (String message, Throwable cause) {super (message, cause) } public BusinessException (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {super (message, cause, enableSuppression, writableStackTrace);} public String getCode () {return code;} public void setCode (String code) {this.code = code } @ Override public String getMessage () {return super.getMessage ();} @ Override public String toString () {return this.code + ":" + this.getMessage ();}}
The system exception SystemException class is implemented in the same way as the business exception class, inheriting RuntimeException.
Public class SystemException extends RuntimeException {/ * error coding * / private String code; public SystemException () {super ();} public SystemException (String message) {super (message);} public SystemException (String code, String message) {super (message) This.code = code;} public SystemException (Throwable cause) {super (cause);} public SystemException (String message, Throwable cause) {super (message, cause) } public SystemException (String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {super (message, cause, enableSuppression, writableStackTrace);} public String getCode () {return code;} public void setCode (String code) {this.code = code } @ Override public String getMessage () {return super.getMessage ();} @ Override public String toString () {return this.code + ":" + this.getMessage ();}}
ExceptionAdvice class, which is implemented by enhanced Controller annotation @ ControllerAdvice
1. The method name and return type can be defined according to your needs.
two。 Using the annotation @ ExceptionHandler, which is the type of exception caught, we just need to write in the type of exception that needs to be caught.
The specific code implementation of ExceptionAdvice is as follows:
Import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody; @ ControllerAdvicepublic class ExceptionAdvice {public static Logger logger = LoggerFactory.getLogger (ExceptionAdvice.class); @ ResponseBody @ ExceptionHandler (SystemException.class) public Result handleException (Exception e) {logger.error ("system exception Information:", e) Result result = new Result (); if (e instanceof BusinessException) {e = (BusinessException) e; result.setCode (BusinessException) e). GetCode ());} result.setFailed (e.getMessage ()); return result } @ ExceptionHandler (RuntimeException.class) @ ResponseBody public Result handleException (RuntimeException e) {logger.error ("exception message:", e.getMessage ()); Result result = new Result (); result.setStatus (500); result.setMessage (e.getMessage ()); return result } @ ExceptionHandler (BusinessException.class) @ ResponseBody public AjaxJson doBusinessException (Exception e) {AjaxJson ajaxJson = new AjaxJson (); logger.error ("Business exception message:", e.getMessage ()); ajaxJson.setRet (- 1); ajaxJson.setMsg (e.getMessage ()); return ajaxJson;}} Test Code
1. We catch a business exception BusinessException and output aaa
two。 Catch system exception
Throw new SystemException ("aaaa")
3. For other try catch exceptions, this can be caught
The above is all the content of the article "how springboot customizes exceptions and catches them back to the front end". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.