互联网公司接口统一返回
版本说明
spring-boot=2.2.5.RELEASE
相关链接
- lombok 官网:https://projectlombok.org/
接口统一返回
CommonResponse
package top.simba1949.response; import lombok.AllArgsConstructor; import lombok.Data; /** * @Author Theodore * @Date 2020/3/20 15:55 */ @Data @AllArgsConstructor public class CommonResponse { /** 请求结果 */ private Boolean status; /** 错误码,当 status=false 时,需要设置 */ private ErrorCode errorCode; /** 信息,当 status=false 时,存储错误信息;也可以存储其他信息 */ private String message; /** 返回数据 */ private Object data; }
ErrorCode
package top.simba1949.response; /** * @Author Theodore * @Date 2020/3/20 16:14 */ public enum ErrorCode { SYSTEM_SUCCESS("0000000", "成功"), SYSTEM_FAILURE("0000001", "系统异常") ; private String code; private String message; ErrorCode(String code, String message) { this.code = code; this.message = message; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
ResponseBuilder
package top.simba1949.response; /** * @Author Theodore * @Date 2020/3/20 16:40 */ public class ResponseBuilder { /** * 成功:无数据返回 * @return */ public static CommonResponse buildSuccess(){ return new CommonResponse(true, null, null, null); } /** * 成功:有数据返回 * @param data * @return */ public static CommonResponse buildSuccess(Object data){ return new CommonResponse(true, null, null, data); } /** * 失败:无数据返回 * @param errorCode * @param message * @return */ public static CommonResponse buildFailure(ErrorCode errorCode, String message){ return new CommonResponse(false, errorCode, message, null); } /** * 失败:有数据返回 * @param errorCode * @param message * @param data * @return */ public static CommonResponse buildFailure(ErrorCode errorCode, String message, Object data){ return new CommonResponse(false, errorCode, message, data); } }