利用枚举封装返回码和返回信息
1 package com.template.project.util; 2 3 public enum StatusCode { 4 5 SUCCESS(20000, "成功"), FAIL(50000, "失败"), 6 7 REQUEST_PARAM_ERROR(50001, "请求参数错误"); 8 9 private int code; 10 11 private String message; 12 13 StatusCode(int code, String message) { 14 this.code = code; 15 this.message = message; 16 } 17 18 public int getCode() { 19 return code; 20 } 21 22 public void setCode(int code) { 23 this.code = code; 24 } 25 26 public String getMessage() { 27 return message; 28 } 29 30 public void setMessage(String message) { 31 this.message = message; 32 } 33 34 }
封装返回类型,可按需要增加相应的构造方法
1 package com.template.project.util; 2 3 import java.time.LocalDateTime; 4 import java.time.format.DateTimeFormatter; 5 6 import com.alibaba.fastjson.JSONObject; 7 8 public class ResultBuilder<T> { 9 10 public ResultBuilder(T data, int code, String message) { 11 this.result = data; 12 this.code = code; 13 this.message = message; 14 this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 15 } 16 17 public ResultBuilder(T data, StatusCode statusCode) { 18 this.result = data; 19 this.code = statusCode.getCode(); 20 this.message = statusCode.getMessage(); 21 this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 22 } 23 24 public ResultBuilder(T data, StatusCode statusCode, String extendMsg) { 25 this.result = data; 26 this.code = statusCode.getCode(); 27 this.message = statusCode.getMessage() + extendMsg; 28 this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 29 } 30 31 public ResultBuilder(StatusCode statusCode) { 32 this.code = statusCode.getCode(); 33 this.message = statusCode.getMessage(); 34 this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 35 } 36 37 public ResultBuilder(StatusCode statusCode, String extendMsg) { 38 this.code = statusCode.getCode(); 39 this.message = statusCode.getMessage() + extendMsg; 40 this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 41 } 42 43 public ResultBuilder(int code, String message) { 44 this.code = code; 45 this.message = message; 46 this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 47 } 48 49 public ResultBuilder(int code) { 50 this.code = code; 51 this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 52 } 53 54 private int code; 55 56 private String message; 57 58 private T result; 59 60 private String responseTime; 61 62 public int getCode() { 63 return code; 64 } 65 66 public void setCode(int code) { 67 this.code = code; 68 } 69 70 public String getMessage() { 71 return message; 72 } 73 74 public void setMessage(String message) { 75 this.message = message; 76 } 77 78 public T getResult() { 79 return this.result; 80 } 81 82 public void setResult(T result) { 83 this.result = result; 84 } 85 86 public String getResponseTime() { 87 return responseTime; 88 } 89 90 public void setResponseTime(String responseTime) { 91 this.responseTime = responseTime; 92 } 93 94 public JSONObject toJSONObject() { 95 JSONObject out = new JSONObject(); 96 out.put("code", code); 97 out.put("message", message); 98 out.put("result", result); 99 out.put("responseTime", responseTime); 100 return out; 101 } 102 103 @Override 104 public String toString() { 105 return toJSONObject().toString(); 106 } 107 108 }
接下来看看测试结果
1 public class Test { 2 3 public static void main(String[] args) { 4 JSONObject jobj = new JSONObject(); 5 jobj.put("name", "wangbo"); 6 jobj.put("age", 29); 7 ResultBuilder<JSONObject> resultBuilder = new ResultBuilder<>(jobj, StatusCode.SUCCESS); 8 System.out.println(resultBuilder); 9 } 10 11 }
执行结果为
{"result":{"name":"wangbo","age":29},"code":20000,"responseTime":"2018-09-04 11:00:09","message":"成功"}
统一响应格式有助于系统间调用和前端解析。