001/* 002 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.dromara.warm.flow.core.dto; 017 018import java.io.Serializable; 019 020/** 021 * 响应信息主体 022 * 023 * @author ruoyi 024 */ 025public class ApiResult<T> implements Serializable 026{ 027 private static final long serialVersionUID = 1L; 028 029 /** 成功 */ 030 public static final int SUCCESS = 200; 031 032 /** 失败 */ 033 public static final int FAIL = 500; 034 035 private int code; 036 037 private String msg; 038 039 private T data; 040 041 public static <T> ApiResult<T> ok() 042 { 043 return restResult(null, SUCCESS, "操作成功"); 044 } 045 046 public static <T> ApiResult<T> ok(T data) 047 { 048 return restResult(data, SUCCESS, "操作成功"); 049 } 050 051 public static <T> ApiResult<T> ok(T data, String msg) 052 { 053 return restResult(data, SUCCESS, msg); 054 } 055 056 public static <T> ApiResult<T> fail() 057 { 058 return restResult(null, FAIL, "操作失败"); 059 } 060 061 public static <T> ApiResult<T> fail(String msg) 062 { 063 return restResult(null, FAIL, msg); 064 } 065 066 public static <T> ApiResult<T> fail(T data) 067 { 068 return restResult(data, FAIL, "操作失败"); 069 } 070 071 public static <T> ApiResult<T> fail(T data, String msg) 072 { 073 return restResult(data, FAIL, msg); 074 } 075 076 public static <T> ApiResult<T> fail(int code, String msg) 077 { 078 return restResult(null, code, msg); 079 } 080 081 private static <T> ApiResult<T> restResult(T data, int code, String msg) 082 { 083 ApiResult<T> apiResult = new ApiResult<>(); 084 apiResult.setCode(code); 085 apiResult.setData(data); 086 apiResult.setMsg(msg); 087 return apiResult; 088 } 089 090 public int getCode() 091 { 092 return code; 093 } 094 095 public void setCode(int code) 096 { 097 this.code = code; 098 } 099 100 public String getMsg() 101 { 102 return msg; 103 } 104 105 public void setMsg(String msg) 106 { 107 this.msg = msg; 108 } 109 110 public T getData() 111 { 112 return data; 113 } 114 115 public void setData(T data) 116 { 117 this.data = data; 118 } 119 120 public static <T> Boolean isError(ApiResult<T> ret) 121 { 122 return !isSuccess(ret); 123 } 124 125 public static <T> Boolean isSuccess(ApiResult<T> ret) 126 { 127 return ApiResult.SUCCESS == ret.getCode(); 128 } 129}