当前位置:网站首页>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());
}
}
边栏推荐
- Brief introduction to common pigtails of communication pigtails
- 使用 OKR 进行 HR 数字化转型
- Mysql数据库轻松学07—select语句书写顺序及执行顺序
- Efficient packet processing system based on dpdk
- Mysql database easy to learn 07 - select statement writing order and execution order
- 微信小程序影视评论交流平台系统毕业设计毕设(5)任务书
- 微信小程序影视评论交流平台系统毕业设计毕设(4)开题报告
- 微信小程序影视评论交流平台系统毕业设计毕设(8)毕业设计论文模板
- Mba-day23 at most at least questions - exercises
- 中午不睡觉下午困
猜你喜欢
![[Chapter 17 corner feature detection based on Harris -- actual combat of MATLAB machine learning project]](/img/41/211f6603a6854b427321c50127c2a4.png)
[Chapter 17 corner feature detection based on Harris -- actual combat of MATLAB machine learning project]

Mba-day23 at most at least questions - exercises

Appium interview questions
![[Chapter 13 image compression and reconstruction based on Hoffman -- image processing application of MATLAB deep learning practice]](/img/ac/6f3ce735f52bc44a5dd65e0f2b870c.png)
[Chapter 13 image compression and reconstruction based on Hoffman -- image processing application of MATLAB deep learning practice]
![[phantom engine UE] package error appears! Solutions to findpin errors](/img/d5/6747e20da6a8a4ca461094bd27bbf0.png)
[phantom engine UE] package error appears! Solutions to findpin errors

Mobile app test method
![[chapter 06 MATLAB realizes lung cancer diagnosis based on watershed segmentation]](/img/2f/b2f141bf2f0b1f2f80444d37529a9b.png)
[chapter 06 MATLAB realizes lung cancer diagnosis based on watershed segmentation]

What is a neural network

Ansible profile

微信小程序影視評論交流平臺系統畢業設計畢設(4)開題報告
随机推荐
[Chapter 14 image compression and reconstruction based on principal component analysis -- matlab deep learning practical case]
Right alignment of MathType formula right number in word
idea----bookmark
Object detection -- how to use labelimg annotation tool
Intel history overview
剑指offer 26:树的子结构
微信小程序影视评论交流平台系统毕业设计毕设(6)开题答辩PPT
Array common methods
Games-101 personal summary rasterization
Development of power plant compliance test system with LabVIEW
Qt程序怎么实现选中ListWidget中的某一行为默认选中
What does the redemption confirmation date of financial products mean?
【 NVIDIA development history record 618-01 】
[chapter 07 face QR code recognition based on principal component analysis matlab deep learning practical case]
微信小程序影视评论交流平台系统毕业设计毕设(7)中期检查报告
Rational Rose installation tutorial
rt_ Message queue of thread
Paper notes: multi label learning ackel
使用 OKR 进行 HR 数字化转型
Idea ---- copy and paste