当前位置:网站首页>Global exception handling
Global exception handling
2022-06-22 02:18:00 【YLi_ Jing】
Global exception handling
1、 Response result set encapsulation
public class ResponseResult<T> implements Serializable {
private Integer code;
private String msg;
private T data;
public ResponseResult() {
this.code = AppHttpCodeEnum.SUCCESS.getCode();
this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
}
public ResponseResult(Integer code, T data) {
this.code = code;
this.data = data;
}
public ResponseResult(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResponseResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static ResponseResult errorResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.error(code, msg);
}
public static ResponseResult okResult() {
ResponseResult result = new ResponseResult();
return result;
}
public static ResponseResult okResult(int code, String msg) {
ResponseResult result = new ResponseResult();
return result.ok(code, null, msg);
}
public static ResponseResult okResult(Object data) {
ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
if (data != null) {
result.setData(data);
}
return result;
}
public static ResponseResult errorResult(AppHttpCodeEnum enums) {
return setAppHttpCodeEnum(enums, enums.getMsg());
}
public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg) {
return setAppHttpCodeEnum(enums, msg);
}
public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
return okResult(enums.getCode(), enums.getMsg());
}
private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg) {
return okResult(enums.getCode(), msg);
}
public ResponseResult<?> error(Integer code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(Integer code, T data) {
this.code = code;
this.data = data;
return this;
}
public ResponseResult<?> ok(Integer code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
return this;
}
public ResponseResult<?> ok(T data) {
this.data = data;
return this;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
2、 Exception response state enumeration class
public enum AppHttpCodeEnum {
/** * Successful operation */
SUCCESS(200," Successful operation "),
SYSTEM_ERROR(504," Unknown system error "),
LOGIN_ERROR(505," The divisor cannot be zero 0");
final int code;
final String msg;
AppHttpCodeEnum(int code, String errorMessage){
this.code = code;
this.msg = errorMessage;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
3、 Custom global exception class
public class SystemException extends RuntimeException{
private int code;
private String msg;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public SystemException(AppHttpCodeEnum httpCodeEnum) {
super(httpCodeEnum.getMsg());
this.code = httpCodeEnum.getCode();
this.msg = httpCodeEnum.getMsg();
}
}
4、 Configure global exception handling
/** * Global exception handling * @author YLi_Jing */
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(SystemException.class)
public ResponseResult systemExceptionHandler(SystemException e){
// Get the prompt information from the exception object to encapsulate the return
return ResponseResult.errorResult(e.getCode(),e.getMsg());
}
@ExceptionHandler(Exception.class)
public ResponseResult exceptionHandler(Exception e){
// Get the prompt information from the exception object to encapsulate the return
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
}
}
边栏推荐
- What is your understanding of interface testing?
- Appium interview questions
- 论文笔记: 多标签学习 ACkEL
- Atguigu---- list rendering
- Minecraft 1.18.2 biochemical 8 module version 1.3 3D objects + more complex villages
- Google Earth engine (GEE) - line chart of time series image combining VCI index and TCI temperature (Guatemala and El Salvador as examples)
- 目标检测之——labelImg标注工具使用方法
- What is a neural network
- atguigu----收集表单数据
- Informer有什么
猜你喜欢

Mysql数据库轻松学06—数据分析师常用:数据查询语言DQL之单表查询

Flexer series: indexedstack in Flexer

Games-101 personal summary transformation

Chapter 12 panoramic image mosaic based on block matching -- Application of MATLAB in-depth learning and actual combat image processing

微信小程序影视评论交流平台系统毕业设计毕设(8)毕业设计论文模板

微信小程序影视评论交流平台系统毕业设计毕设(4)开题报告
![[chapter 04 answer sheet recognition based on Hough change]](/img/2a/83ac4875ec7f61e28eb81175d35142.png)
[chapter 04 answer sheet recognition based on Hough change]

sql server递归查询

Completion of graduation design of wechat small program film and television review and exchange platform system (4) opening report

excel常用快捷键excel快捷键汇总
随机推荐
[essay] the Expo that studied the RN ecology for one day yesterday is really awesome. It works well from development and construction to deployment.
PostgreSQL fetches data according to the size of the time field
[chapter 04 answer sheet recognition based on Hough change]
NVIDIA written interview questions sorting DIY
Completion of graduation design of wechat small program film and television review and exchange platform system (4) opening report
What is a neural network
[chapter 02 weight adaptive image denoising technology based on Morphology - full system matlab intelligent driving in-depth learning]
rt_thread线程管理
Ansible inventory host list
Zhongang Mining Co., Ltd.: fluorite is a scarce resource with enhanced attributes, and there may be a gap between supply and demand in the future
说说你对接口测试的理解?
Ansible profile
Rational Rose installation tutorial
Mysql数据库轻松学07—select语句书写顺序及执行顺序
zap语法糖
Atguigu---- filter
Intel written test questions DIY
The "cloud" end of the 6th world intelligence conference will be held soon
SQL Server recursive query
Mobile app test method