Java 数据返回接口封装

时间:2024-01-17 13:44:14

enum StatusCode

package com.lee.utils;

public enum StatusCode {
SUCCESS(20000, "成功"), FALL(50000, "失败"), REQUEST_PARAM_ERROR(50001, "请求参数错误"); private int code; private String message; StatusCode(int code, String message) {
this.code = code;
this.message = message;
} public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} }

class ResultBuilder<T>

package com.lee.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import com.alibaba.fastjson.JSONObject; public class ResultBuilder<T> { public ResultBuilder(T data, int code, String message) {
this.result = data;
this.code = code;
this.message = message;
this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} public ResultBuilder(T data, StatusCode statusCode) {
this.result = data;
this.code = statusCode.getCode();
this.message = statusCode.getMessage();
this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} public ResultBuilder(T data, StatusCode statusCode, String extendMsg) {
this.result = data;
this.code = statusCode.getCode();
this.message = statusCode.getMessage() + extendMsg;
this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} public ResultBuilder(StatusCode statusCode) {
this.code = statusCode.getCode();
this.message = statusCode.getMessage();
this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} public ResultBuilder(StatusCode statusCode, String extendMsg) {
this.code = statusCode.getCode();
this.message = statusCode.getMessage() + extendMsg;
this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} public ResultBuilder(int code, String message) {
this.code = code;
this.message = message;
this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} public ResultBuilder(int code) {
this.code = code;
this.responseTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} private int code;
private String message;
private T result;
private String responseTime; public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public T getResult() {
return result;
} public void setResult(T result) {
this.result = result;
} public String getResponseTime() {
return responseTime;
} public void setResponseTime(String responseTime) {
this.responseTime = responseTime;
} public JSONObject toJSONObject() {
JSONObject out = new JSONObject();
out.put("code", code);
out.put("message", message);
out.put("result", result);
out.put("responseTime", responseTime);
return out;
} public String toString() {
return toJSONObject().toString();
}
}