标签:err stc advice exception 第一步 data request util src
CustomExtHandler.java
package net.cyb.demo.handler; import net.cyb.demo.utils.JsonData; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.servlet.http.HttpServletRequest; /** * 标记这是一个异常处理类 */ @RestControllerAdvice public class CustomExtHandler { @ExceptionHandler(value = Exception.class) JsonData HandlerException(Exception e, HttpServletRequest request){ return JsonData.buildError(-2,"服务端出问题了"); } }
JsonData.java
package net.cyb.demo.utils; /** * 接口返回工具类 */ public class JsonData { private int code; private Object data; private String msg; public JsonData() { } public JsonData(int code, Object data) { this.code = code; this.data = data; } public JsonData(int code, Object data, String msg) { this.code = code; this.data = data; this.msg = msg; } public static JsonData buildSuccess(Object data) { return new JsonData(0, data); } public static JsonData buildError(String msg) { return new JsonData(-1, "", msg); } public static JsonData buildError(int code, String msg) { return new JsonData(code, "", msg); } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return "JsonData{" + "code=" + code + ", data=" + data + ", msg=‘" + msg + ‘\‘‘ + ‘}‘; } }
标签:err stc advice exception 第一步 data request util src
原文地址:https://www.cnblogs.com/chenyanbin/p/13237872.html