java 统一信息返回类_Java 前后端 统一返回数据格式

本文介绍了如何在Java应用中设计统一的前后端交互数据格式,包括定义基本状态码枚举和创建ActionResponse类来封装返回信息,确保服务之间的交互标准化。
摘要由CSDN通过智能技术生成

1 概述

现在前后端交互,基本上都有统一的返回数据结构,因此我特地总结了相关知识,形成这篇博客。

2 状态码定义

/**

* description: 基本返回状态码

*/

public enum RespBasicCode {

/**

* 4xx 客户端异常 5xx服务器异常

*/

SUCCESS("200", "成功"),

PARAMETER_ERROR("400", "参数异常"),

BAD_REQUEST("400", "无效的请求"),

ERROR("500", "异常错误"),

/*--------根据业务实际情况,可以自己定义返回的状态码-------*/

;

/**

* 返回状态码

*/

private String code;

/**

* 返回描述

*/

private String result;

RespBasicCode(String code, String result) {

this.code = code;

this.result = result;

}

public String getCode() {

return code;

}

public String getResult() {

return result;

}

/**

* 通过code 获取RespBasicCode对象

*

* @param code 状态码

* @return RespBasicCode对象

*/

public static RespBasicCode getRespBasicCodeByCode(String code) {

if (code == null || "".equals(code)) {

return null;

}

for (RespBasicCode respBasicCode : RespBasicCode.values()) {

if (respBasicCode.getCode().equals(code)) {

return respBasicCode;

}

}

return null;

}

/**

* 通过code 获取resultDes

*

* @param code 状态码

* @return resultDes

*/

public static String getResultDesByCode(String code) {

if (code == null || "".equals(code)) {

return null;

}

for (RespBasicCode respBasicCode : RespBasicCode.values()) {

if (respBasicCode.getCode().equals(code)) {

return respBasicCode.getResult();

}

}

return null;

}

}

3 统一返回数据结构

/**

* description: 服务之间交互统一响应

* 返回划分为2部分:分别是头部和实体信息

* 头部返回状态为200,则从Body里面取数据

* 如果头部返回异常状态码,则从头部取出错误信息

*/

public class ActionResponse {

/**

* 返回的头部信息

*/

private Head head = new Head();

/**

* 返回主体信息

*/

private Body body = new Body();

/**

* 返回成功,没有data数据 结果:

* {

* "head": {

* "code": "200",

* "result": "成功"

* },

* "body": null

* }

*

* @param Body中 data类型

* @return ActionResponse

*/

public static ActionResponse success() {

return new ActionResponse(RespBasicCode.SUCCESS.getCode(),RespBasicCode.SUCCESS.getResult(),null);

}

/**

* 返回成功,并且带有数据 结果:

* {

* "head": {

* "code": "200",

* "result": "成功"

* },

* "body": [

* "你好 宇宙"

* ]

* }

*

* @param data 数据

* @param Body中 data类型

* @return ActionResponse

*/

public static ActionResponse success(T data) {

return new ActionResponse<>(RespBasicCode.SUCCESS.getCode(),RespBasicCode.SUCCESS.getResult(), data);

}

/**

* 返回失败,自定义状态码,并且没有任何数据 结果:

*{

* "head": {

* "code": "400",

* "result": "参数异常"

* },

* "body": null

* }

*

* @param respCode 状态码对象

* @param Body中 data类型

* @return ActionResponse

*/

public static ActionResponse fail(RespBasicCode respCode) {

return new ActionResponse<>(respCode.getCode(),respCode.getResult(),null);

}

/**

* 返回失败,自定义状态码,并且封装数据 结果:

*{

* "head": {

* "code": "400",

* "result": "参数异常"

* },

* "body": [

* "你好 宇宙"

* ]

* }

*

* @param respCode respCode 状态码对象

* @param data 数据

* @param Body中 data类型

* @return ActionResponse

*/

public static ActionResponse fail(RespBasicCode respCode, T data) {

return new ActionResponse<>(respCode.getCode(), respCode.getResult(), data);

}

/**

* 自定义返回信息

*

* @param code head中的code

* @param result head中的result

* @param data body中的数据

* @param Body中 data类型

* @return ActionResponse

*/

public static ActionResponse custom(String code,String result, T data){

return new ActionResponse<>(code,result,data);

}

/**

* 真正的数据源泉,所有方法均是调用这个构造器

*

* @param code head中的具体的状态码

* @param result head中的描述信息

*/

private ActionResponse(String code, String result, T data) {

this.head.code = code;

this.head.result = result;

this.body.data = data;

}

public Head getHead() {

return head;

}

public T getBody() {

return body.getData();

}

/**

* 返回的实体信息

*/

private class Body {

/**

* 数据域,如果是分页直接为PageResult即可

*/

private T data;

public T getData() {

return data;

}

}

/**

* 响应的头部

*/

private class Head {

/**

* 状态码

*/

private String code;

/**

* 结果描述

*/

private String result;

public String getCode() {

return code;

}

public String getResult() {

return result;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值