统一异常处理
1.创建Result类
package com.markLuo.domain; /** * http请求返回的最外层对象 * Created by markLuo * 2018-01-21 13:34 */ public class Result<T> { /** 错误码. */ private Integer code; /** 提示信息. */ private String msg; /** 具体的内容. */ private T data; 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.创建ResultUtil
package com.markLuo.utils; import com.markLuo.domain.Result; /** * Created by markLuo * 2018-01-21 13:39 */ public class ResultUtil { public static Result success(Object object) { Result result = new Result(); result.setCode(0); result.setMsg("成功"); result.setData(object); return result; } public static Result success() { return success(null); } public static Result error(Integer code, String msg) { Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } }
3.使用