首页 技术 正文
技术 2022年11月14日
0 收藏 307 点赞 2,316 浏览 3146 个字

利用枚举封装返回码和返回信息

 package com.template.project.util; public enum StatusCode {     SUCCESS(20000, "成功"), FAIL(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;
} }

封装返回类型,可按需要增加相应的构造方法

 package com.template.project.util; 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 this.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;
} @Override
public String toString() {
return toJSONObject().toString();
} }

接下来看看测试结果

 public class Test {     public static void main(String[] args) {
JSONObject jobj = new JSONObject();
jobj.put("name", "wangbo");
jobj.put("age", 29);
ResultBuilder<JSONObject> resultBuilder = new ResultBuilder<>(jobj, StatusCode.SUCCESS);
System.out.println(resultBuilder);
} }

执行结果为

{"result":{"name":"wangbo","age":29},"code":20000,"responseTime":"2018-09-04 11:00:09","message":"成功"}

统一响应格式有助于系统间调用和前端解析。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848