定义统一的返回格式(controller)
一:单独创建一个类来表示返回结果
package com.jk51.commons.dto; /** * Created by Administrator on 2017/6/13. */ public class PacketHead { public static final String STATUS_SUCCESS = "OK";//成功 public static final String STATUS_ERROR = "ERROR";//成功 private String status; //返回状态 private String msg; //返回信息 public PacketHead() { status = STATUS_SUCCESS; } public PacketHead(String status, String result) { this.status = status; this.msg = msg; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
二:创建返回类ResultData 以后controller可以统一采用此类作为返回结果
/** * 返回结果封装 * Created by Administrator on 2018/7/3. */ public class ResultData extends PacketHead{ private Object data; public ResultData(){ this.setStatus(STATUS_SUCCESS); } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
用法如图实例
作者:
不二尘
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。