公共异常处理
返回码定义类返回结果实体类异常处理类
为了使我们的代码更容易维护,每个微服我们创建一个类集中处理异常
返回码定义类
package entity
;
public class StatusCode {
public static final int OK
=20000;
public static final int ERROR
=20001;
public static final int LOGINERROR
=20002;
public static final int ACCESSERROR
=20003;
public static final int REMOTEERROR
=20004;
public static final int REPERROR
=20005;
}
返回结果实体类
package entity
;
public class Result {
private boolean flag
;
private Integer code
;
private String message
;
private Object data
;
public Result() {
}
public Result(boolean flag
, Integer code
, String message
, Object data
) {
this.flag
= flag
;
this.code
= code
;
this.message
= message
;
this.data
= data
;
}
public Result(boolean flag
, Integer code
, String message
) {
this.flag
= flag
;
this.code
= code
;
this.message
= message
;
}
public Integer
getCode() {
return code
;
}
public void setCode(Integer code
) {
this.code
= code
;
}
public boolean isFlag() {
return flag
;
}
public void setFlag(boolean flag
) {
this.flag
= flag
;
}
public String
getMessage() {
return message
;
}
public void setMessage(String message
) {
this.message
= message
;
}
public Object
getData() {
return data
;
}
public void setData(Object data
) {
this.data
= data
;
}
}
异常处理类
package com
.tensquare
.base
.controller
;
import entity
.Result
;
import entity
.StatusCode
;
import org
.springframework
.web
.bind
.annotation
.ExceptionHandler
;
import org
.springframework
.web
.bind
.annotation
.RestControllerAdvice
;
@RestControllerAdvice
public class LabelExceptionHandler {
@ExceptionHandler(value
= Exception
.class)
public Result
error(Exception e
){
e
.printStackTrace();
return new Result(false,StatusCode
.ERROR
, e
.getMessage());
}
}
转载请注明原文地址: https://yun.8miu.com/read-58067.html