Java 数据返回接口封装
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();
}
}
Java 数据返回接口封装的更多相关文章
- 【开源项目12】Retrofit – Java(Android) 的REST 接口封装类库
Retrofit官网:http://square.github.io/retrofit/ Retrofit 和Java领域的ORM概念类似, ORM把结构化数据转换为Java对象,而Retrofit ...
- Retrofit – Java(Android) 的REST 接口封装类库
Retrofit 和Java领域的ORM概念类似, ORM把结构化数据转换为Java对象,而Retrofit 把REST API返回的数据转化为Java对象方便操作.同时还封装了网络代码的调用. 例如 ...
- java—将查询的结果封装成List<Map>与用回调函数实现数据的动态封装(44)
手工的开始QueryRunner类.实现数据封装: MapListHandler MapHandler BeanListHandler BeanHandler 第一步:基本的封装测试 写一个类,Que ...
- 基于Zabbix API文档二次开发与java接口封装
(继续贴一篇之前工作期间写的经验案例) 一. 案例背景 我负责开发过一个平台的监控报警模块,基于zabbix实现,需要对zabbix进行二次开发. Zabbix官方提供了Rest ...
- java:Session(概述,三层架构实例(实现接口封装JDBC),Session实现简单购物车实例)
1.Session概述: Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存 ...
- spring boot 接口返回值封装
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- java读取PHP接口数据的实现方法(四)
PHP文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...
- 聚合数据实名认证接口-java方法
只需要填入购买的APPKEY,然后直接调用方法JuheDemo.info(user_name, anchor_card);传入姓名和身份证号,根据获取的返回参数进行拆分,如res=1说明正确. //进 ...
- Java学习-013-文本文件读取实例源代码(两种数据返回格式)
此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 1.读取的文本文件内容以一维数组[LinkedList<String>]的形式返回,源代 ...
随机推荐
- jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题
jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...
- css 图片置灰
.filterGray{ -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(10 ...
- C++中继承与抽象类
继承语法格式如下: class 子类名称 : 继承方式(public private protected 三种) 父类名称 纯虚函数格式: virtual 返回值类型 函数名(参数列表)= 0:含有纯 ...
- linux中启动 java -jar 后台运行程序
直接用java -jar xxx.jar,当退出或关闭shell时,程序就会停止掉.以下方法可让jar运行后一直在后台运行. 1. java -jar xxx.jar & 说明: 在末尾加入 ...
- MacBookPro磁盘空间不够
256G的SSD还是快被占满了,剩余12G,本来一切运行正常. 要往U盘里拷点资料,突然电脑就罢工了,cleanMyMac 显示磁盘容量剩余 1.8G. finder 罢工,无法重启,无法强退. 无法 ...
- day25:接口类和抽象类
1,接口类和抽象类知识背景:一开始在程序界是没有这些概念的,后来就流行了一种语言叫做JAVA,JAVA是一种典型的纯面向对象的语言,JAVA写的程序都是非常适用于更庞大的项目,很多程序员就总结出了一些 ...
- box-shodow的使用
text-shadow是给文本添加阴影效果,box-shadow是给元素块添加周边阴影效果.随着HTML5和CSS3的普及,这一特殊效果使用越来越普遍. 基本语法是{box-shadow:[inset ...
- Mockito单元测试实战
最近使用Mockito完成了几个简单的测试,写个博客mark一下: 第一种模拟web请求 @SpringBootTest @RunWith(SpringRunner.class) @WebAppCon ...
- 当使用eclipse将项目部署到Tomcat时,提示Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modul
原因: 此版本选择过高.当出现此错误时,直接对项目可能无法进行修改.可以通过修改项目的配置文件来达到目的. \workspace\项目名称\.settings\org.eclipse.wst.comm ...
- mysql授权用户,撤销用户,撤销权限基本操作
进入mysql库 use mysql; mysql> select host,user from user; +-----------+---------------+ | host | use ...