java中伺服器響應報文格式統一
阿新 • • 發佈:2018-11-23
Result:服務端響應報文格式
package com.demo.platform.common.result.base; import com.alibaba.fastjson.annotation.JSONField; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; @ApiModel("服務端響應報文格式") public class Result<T> implements Serializable { private static final long serialVersionUID = -1791232625706807828L; @ApiModelProperty("響應頭資料") @JSONField( name = "head", ordinal = 0 ) private ResultHead head; @ApiModelProperty("響應體資料") @JSONField( name = "body", ordinal = 1 ) protected T body; public Result() { } public T getBody() { return this.body; } public void setBody(T body) { this.body = body; } public ResultHead getHead() { return this.head; } public void setHead(ResultHead head) { this.head = head; } public String toString() { return "Result{head=" + this.getHead().toString() + ",data=" + this.body + '}'; } }
ResultHelper:響應報文封裝
//伺服器響應報文 package com.demo.platform.common.result.base; import com.demo.platform.common.result.enums.ResultCodeEnum; import io.swagger.annotations.ApiModel; import java.io.Serializable; import javax.xml.bind.annotation.XmlRootElement; @ApiModel( description = "服務端響應報文格式" ) @XmlRootElement public class ResultHelper<T> implements Serializable { public ResultHelper() { } public static <T> Result setResultData(ResultCodeEnum resultEnum, String message) { ResultHead head = new ResultHead(); head.setCode(resultEnum.getKey() + ""); head.setMessage(resultEnum.getDesc()); head.setUrl(""); head.setSubCode(""); head.setSubMessage(message); Result result = new Result(); result.setHead(head); result.setBody(""); return result; } public static <T> Result setResultData(ResultCodeEnum resultEnum, T data) { ResultHead head = new ResultHead(); head.setCode(resultEnum.getKey() + ""); head.setMessage(resultEnum.getDesc()); head.setUrl(""); head.setSubCode(""); head.setSubMessage(""); Result result = new Result(); result.setHead(head); result.setBody(data); return result; } public static <T> Result setResultData(T data) { return setResultData(ResultCodeEnum.SUCCESS, data); } public static Result setResultData(ResultCodeEnum resultEnum) { ResultHead head = new ResultHead(); head.setCode(resultEnum.getKey() + ""); head.setMessage(resultEnum.getDesc()); head.setUrl(""); head.setSubCode(""); head.setSubMessage(""); Result result = new Result(); result.setHead(head); result.setBody(""); return result; } }
ResultCodeEnum :狀態碼列舉類
//狀態碼列舉 package com.demo.platform.common.result.enums; public enum ResultCodeEnum { SUCCESS(200, "請求成功"), SUCCESS_BUT_NO_DATA(200, "請求成功,但是沒有符合要求的資料"), SUCCESS_BUT_BUSINESS_DEAL(200, "請求成功,但是業務處理上有處理"), SUCCESS_BUT_CHANNEL_NOT_ALLOWED(200, "請求成功,但是該操作不允許在當前渠道執行"), BAD_REQUEST(400, "請求引數錯誤"), BAD_REQUEST_ILLEGAL_PARAM(400, "請求引數錯誤"), BAD_REQUEST_AUTH(401, "認證失敗"), BAD_REQUEST_AUTH_NO_SESSION(401, "認證失敗,使用者未登入"), BAD_REQUEST_FORBIDDEN(403, "資源無許可權"), BAD_REQUEST_FORBIDDEN_FALSE_CHANNEL(403, "資源無許可權,該操作不允許在當前渠道執行"), BAD_REQUEST_NOTFOUND(404, "資源不存在"), INTERNAL_SERVER_ERROR(500, "伺服器錯誤"); private int key; private String desc; private ResultCodeEnum(int key, String desc) { this.key = key; this.desc = desc; } public String getDesc() { return this.desc; } public void setDesc(String desc) { this.desc = desc; } public int getKey() { return this.key; } public void setKey(int key) { this.key = key; } }