当前位置:网站首页>SSM integration exception handler and project exception handling scheme
SSM integration exception handler and project exception handling scheme
2022-07-02 16:20:00 【Xia Zhi 121】
Catalog
Two 、 Project exception handling scheme
Project exception classification
Project exception handling scheme
One 、 Exception handler
In the process of program development, it is inevitable to encounter abnormal phenomena
The common locations and causes of abnormal phenomena are as follows :
■ Exceptions thrown inside the framework : Caused by non-conforming use
■ Exceptions thrown by the data layer : Due to external server failure ( for example : Server access timeout )
■ Exceptions thrown by the business layer : Due to business logic writing errors ( for example : Traverse business writing operations , Cause index exceptions, etc )
■ Exceptions thrown by the presentation layer : Due to data collection 、 Verification and other rules lead to ( for example : Mismatched data types cause exceptions )
■ Exception thrown by tool class : Because the writing of tool books is not rigorous and robust enough, it leads to ( for example : The connection that needs to be released has not been released for a long time, etc )
1、 Exceptions occur at all levels , Which layer is the exception handling code written on ?
All exceptions are thrown to the presentation layer for processing
2、 The presentation layer handles exceptions , Each method is written separately , The amount of code written is huge and the meaning is not strong , How to solve ?
AOP thought
Exception handler
Centralized 、 Handle exceptions in the project in a unified way
@RestControllerAdvice
public class ProjectExceptionAdvice{
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
return new Result(666,null);
}
}name :@RestControllerAdvice
type : Class annotation
Location :Rest Style development controller enhancement class definition
effect : by Rest Enhance the controller class developed in style
Example :
@RestControllerAdvice public class ProjectExceptionAdvice{ }explain : This annotation comes with @ResponseBody Annotations and @Component annotation , Have corresponding functions
name :@ExceptionHandler
type : Method notes
Location : Controller method dedicated to exception handling
effect : Set the handling scheme of the specified exception , The function is equivalent to the controller method , Terminate the execution of the original controller after an exception occurs , And go to the current method to execute
Example :
@RestControllerAdvice public class ProjectExceptionAdvice{ @ExceptionHandler(Exception.class) public Result doException(Exception ex){ return new Result(666,null); } }explain : Such methods can vary according to the exceptions handled , Make multiple methods to deal with the corresponding exceptions respectively
Exception handler processing effect comparison :
When there is no abnormality :

When there is abnormal :

Two 、 Project exception handling scheme
Project exception classification
Business exceptions (BusinessException)
Exceptions caused by standardized user behavior
Exceptions caused by irregular user behavior
System exception (SystemException)
Predictable and unavoidable exceptions during project operation
Other anomalies (Exception)
Unexpected exceptions by programmers
Project exception handling scheme
Business exceptions (BusinessException)
Send the corresponding message to the user , Remind the standard operation
System exception (SystemException)
Send a fixed message to the user , Appease users
Send specific messages to O & M personnel , Remind maintenance
Log
Other anomalies (Exception)
Send a fixed message to the user , Appease users
Send specific messages to programmers , Remind maintenance ( Within the expected range )
Log
Project exception handling
1、 Custom item exception system level exception
// Custom exception handler , Used to encapsulate exception information , Classify exceptions
public class SystemException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code, String message) {
super(message);
this.code = code;
}
public SystemException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}2、 Custom item business level exception
// Custom exception handler , Used to encapsulate exception information , Classify exceptions
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}3、 Custom exception code ( Keep adding )
public class Code {
public static final Integer SYSTEM_UNKNOW_ERROR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERROR = 50002;
public static final Integer PROJECT_VALIDATE_ERROR = 60001;
public static final Integer PROJECT_BUSINESS_ERROR = 60002;
}4、 Trigger custom exception
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public Book getById(Integer id) {
// Simulate business exceptions , Wrap as a custom exception
if(id < 0){
throw new BusinessException(Code.PROJECT_BUSINESS_ERROR," Do not operate illegally !");
}
return bookDao.getById(id);
}
}5、 Intercept and handle exceptions
//@RestControllerAdvice Used to identify that the current class is REST Exception handler corresponding to style
@RestControllerAdvice
public class ProjectExceptionAdvice {
//@ExceptionHandler Used to set the exception type corresponding to the current processor class
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
// Log
// Send a message to O & M
// Send mail to developers ,ex Object to the developer
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getCode(),null,ex.getMessage());
}
// In addition to custom exception handlers , Keep right Exception Exception handling of type , Used to handle unexpected exceptions
@ExceptionHandler(Exception.class)
public Result doOtherException(Exception ex){
// Log
// Send a message to O & M
// Send mail to developers ,ex Object to the developer
return new Result(Code.SYSTEM_UNKNOW_ERROR,null," The system is busy , Please contact the Administrator !");
}
}6、 Exception handler effect comparison


边栏推荐
- 分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
- Boot transaction usage
- Pattern matching extraction of specific subgraphs in graphx graph Computing Practice
- Leetcode -- number of palindromes
- Maui学习之路(三)--Winui3深入探讨
- Route service grid traffic through two-level gateway design
- [fluent] dart language (DART language features | JIT instant compilation | AOT static compilation)
- 数学分析_笔记_第5章:一元微分学
- 数据湖(十一):Iceberg表数据组织与查询
- Add user-defined formula (time sharing t+0) to mobile app access as an example
猜你喜欢

Application of visualization technology in Nebula graph

PyC file decompile

Remove the underline in router link
![[5g NR] RRC connection release](/img/f3/a03f5124493b1c03e7336c55871330.png)
[5g NR] RRC connection release

dried food! Understand the structural vulnerability of graph convolution networks

Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)

2020.4.12 byte written test questions B DP D monotone stack

JS learning notes - first acquaintance

Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)

一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
随机推荐
数仓中的维度表与事实表
微信v3native支付设置的结束时间处理办法
Register as a harmonios developer and install deveco studio 3.0 beta2 for harmonios
Yyds dry inventory method of deleting expired documents in batch
月报总结|Moonbeam6月份大事一览
The light of ideal never dies
Seal Library - installation and introduction
通过两级网关设计来路由服务网格流量
理想之光不灭
手机app通达信添加自定义公式(分时T+0)为例子讲解
Crawl the information of national colleges and universities in 1 minute and make it into a large screen for visualization!
(practice C language every day) the sum of the nearest three numbers
Recalling the college entrance examination and becoming a programmer, do you regret it?
JS learning notes - process control
SQLServer查询哪些索引利用率低
After the win10 system is upgraded for a period of time, the memory occupation is too high
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
idea jar包冲突排查
MySQL calculates the data within the longitude and latitude range
How to import a billion level offline CSV into Nepal graph



