接口返回统一格式为:
1 { 2 'result': 1, 3 'errorCode': '', 4 'errorMsg': '成功', 5 'data': {'a': 111, 'b': '222'} 6 }
代码如下:
1 import java.io.Serializable; 2 import java.util.HashMap; 3 import java.util.Map; 4 5 public class ResultMessage implements Serializable { 6 /** 7 * 返回格式: 8 * {'result': 1, 9 * 'errorCode': '', 10 * 'errorMsg': '成功', 11 * 'data': {'a': 111, 'b': '222'}} 12 */ 13 private int result = 0; 14 private String errorCode = ""; 15 private String errorMsg; 16 private Map<String, Object> data = new HashMap(); 17 18 private ResultMessage() { 19 } 20 21 public static ResultMessage create() { 22 return new ResultMessage(); 23 } 24 25 public int getResult() { 26 return this.result; 27 } 28 29 public String getErrorCode() { 30 return this.errorCode; 31 } 32 33 public String getErrorMsg() { 34 return this.errorMsg; 35 } 36 37 public void raise(int result, String errorCode,String errorMsg) { 38 this.result = result; 39 this.errorCode = errorCode; 40 this.errorMsg = errorMsg; 41 } 42 43 public void put(String key, Object obj) { 44 this.data.put(key, obj); 45 } 46 47 public Map<String, Object> getData() { 48 return this.data; 49 } 50 } 51 52 53 ResultMessage resultMessage = ResultMessage.create(); 54 55 56 resultMessage.put("a", 111); 57 resultMessage.put("b", "222"); 58 resultMessage.raise(1,"", "成功");