Java 利用枚举封装接口返回 JSON 结构
利用枚举封装返回码和返回信息
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":"成功"}
统一响应格式有助于系统间调用和前端解析。
Java 利用枚举封装接口返回 JSON 结构的更多相关文章
- java通过url调用远程接口返回json数据
java通过url调用远程接口返回json数据,有用户名和密码验证, 转自 https://blog.csdn.net/wanglong1990421/article/details/78815856 ...
- C#调用接口返回json数据中含有双引号 或其他非法字符的解决办法
这几天,调用别人接口返回json数据含有特殊符号(双引号),当转换成json对象总是报错, json字符格式如下 { "BOXINFO":[ { ", "ITE ...
- Postman Postman测试接口之JSON结构化数据提交
Postman测试接口之JSON结构化数据提交 by:授客 QQ:1033553122 本文主要是针对结构比较复杂一点的JSON协议数据的提交做个简单说明 举例: 用户下订单接口 接口方向 客户端 ...
- 【Golang 接口自动化04】 解析接口返回JSON串
前言 上一次我们一起学习了如何解析接口返回的XML数据,这一次我们一起来学习JSON的解析方法. JSON(Javascript Object Notation)是一种轻量级的数据交换语言,以文字为基 ...
- 接口返回json
use Mojolicious::Lite; use JSON qw/encode_json decode_json/; # /foo?user=sri get '/api' => sub { ...
- Java 利用枚举实现单例模式
引言 单例模式比较常见的实现方法有懒汉模式,DCL模式公有静态成员等,从Java 1.5版本起,单元素枚举实现单例模式成为最佳的方法. Java枚举 基本用法 枚举的用法比较多,本文主要旨在介绍利用枚 ...
- WebApi接口返回json,xml,text纯文本等
[Route("api/Message/MessageList/")] [HttpGet] public HttpResponseMessage MessageList() { R ...
- vue v-time指令封装(接口返回时间戳 在到日期转换)
// 全局时间戳转换指令注册Vue.directive('time',{ bind: function (el,binding) { let getTime = new Date(binding.va ...
- 使用layui异步请求上传图片在tp5.1环境下出现“请对上传接口返回json”的错误的解决方法
正常情况下返回json数据使用return json(); 但是使用layui会报错,然后想到了使用json_encode()包装一下用一个变量接收后,再使用return();返回接收json格式的变 ...
随机推荐
- server 打开失败
server:An unexpected exception was thrown. 当server服务器遇到这样遇到不能料想的错误导致打开失败的情况下,我们可以找到一个com.genuitec.ec ...
- Spring的xml配置文件中约束的必要性 找不到元素 'beans' 的声明
今天在复习Spring MVC框架的时候,只知道xml配置文件中的约束有规范书写格式的作用,所以在配置HandlerMapping对象信息的时候没有加入约束信息之后进行测试,没有遇到问题.后来在配置S ...
- Webmagic 爬虫框架 爬取马蜂窝、携程旅游、汽车之家游记信息
WebMagic学习 遇到的问题 Log4j错误 解决:在src目录下添加配置文件 log4j.properties log4j.rootLogger=INFO, stdout, file log4j ...
- java36
1.Main public static void main(String [ ] args) public:被jvm调用的方法,权限要足够大 static:被jvm调用的方法,不需要创建对象,直接用 ...
- Sorl 4.10 入门合集
Sorl4.10 + Tomcat 7.0 win7环境下的安装 1.首先是到apache官网下载sorl 4.10 ,解压 2.进入路径\solr-4.10.4\example\webapps,拷 ...
- 20175316 盛茂淞 Arrays和String单元测试
Arrays和String单元测试 具体描述: 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arr ...
- 关于U3D的.SDK对接
1,SDK对接原理:https://www.cnblogs.com/msxh/p/7220741.html 2,Unity ADS对接:https://blog.csdn.net/chenluwolf ...
- (PMP)解题技巧和典型题目分析(0903-2班)
1.计算题 ,5 2.概念题,少 3.情景题,很多 C B C D ------------------------------------------------------------------ ...
- suse 11 pip pip3使用过程中遇到的各种问题
在安装完成python3.6后,使用pip3安装某些插件,报如下错误 linux-9qk9:~ # pip3 install ipython pip is configured with locati ...
- python-mysql驱动64位
安装Python-MySQL驱动一直没有成功: https://pypi.python.org/pypi/MySQL-python/1.2.5#downloads 上面网站下的版本安装都不能通过 提示 ...