/**
* 返回给前台页面的值的封装类
* @author x_luwl
*
*/
public class ResponseMessage<T> {
private int code;
private String msg;
private T result;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
public enum ResultEnum {
UNKNOWN_ERROR(99999, "未知错误"),
//1开头为参数
LOCAL_ERROR(10001,"路径访问错误"),
PARAM_ERROR(10002,"路径参数错误"),
PARAM_JIEXI_ERROR(10003,"参数解析错误"),
PARAM_YANZHENG_ERROR(10004,"参数验证错误"),
PARAM_BANGDING_ERROE(10005,"参数绑定失败"),
HTTP_SENDREQUEST_ERROR(20001,"不支持当前请求方法"),
THE_MEDIA_TYPE_ERROE(20002,"不支持当前媒体类型"),
THE_CODE_DO_ERROE(30001,"业务逻辑异常"),
//后台报错返回的给前台的消息台
EXCEPTION_ERROR(21001,"系统繁忙"),
SYSTEM_ERROR(21002,"系统错误"),
SQL_ERROR(21003,"数据库错误");
private Integer code;
private String msg;
private ResultEnum(Integer code, String msg) {
this.code = code;
this.msg=msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 统一返回值工具
* @author x_luwl
*
*/
public class ResultUtil{
private static final Logger LOGGER = LoggerFactory.getLogger(ResultUtil.class);
/**
* 请求成功返回
* @param object
* @return
*/
public static ResponseMessage success(Object object){
ResponseMessage msg=new ResponseMessage();
msg.setCode(200);
msg.setMsg("请求成功");
msg.setResult(object);
LOGGER.info("请求数据成功,数据值为:"+object);
return msg;
}
public static ResponseMessage success(){
LOGGER.info("请求数据成功");
return success(null);
}
public static ResponseMessage error(Integer code,String resultmsg){
ResponseMessage msg=new ResponseMessage();
msg.setCode(code);
msg.setMsg(resultmsg);
return msg;
}
}
使用:
Controller 返回值为ResponseMessage
return ResultUtil.success(obj);