T - 数据类型public class ApiResponse<T> extends Object implements Serializable
成功示例如下:
public ApiResponse<EchoVo> echoObjectByObject(@RequestBody EchoVo echo) {
EchoVo vo = echoService.echoObjectByObject(echo);
return ApiResponse.ok(vo);
}
public ApiResponse<String> updateMain(@RequestBody DemoRobotMainVo vo) {
demoRobotService.updateMain(vo);
return ApiResponse.ok();
}
失败示例如下:
public ApiResponse<String> handleFileUpload(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
try {
BufferedImage image = ImageIO.read(newFile);
if (image != null && image.getWidth() > 0L) {
isLegal = true;
}
} catch (IOException e) {
log.warn("{}", e.getMessage(), e);
return new ApiResponse<>(ApiResponse.FAIL, e.getMessage(), "");
}
return ApiResponse.ok();
}
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
BUSY |
static String |
BUSY_TEXT |
static int |
FAIL |
static String |
FAIL_TEXT |
static int |
SUCCESS |
static String |
SUCCESS_TEXT |
| 构造器和说明 |
|---|
ApiResponse()
默认构造方法,构造一个成功的响应
|
ApiResponse(long status,
String message,
T data)
通过参数构造响应对象
|
ApiResponse(T data)
通过参数构造响应对象
|
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> ApiResponse<T> |
fail(String message)
返回执行失败的响应
|
static <T> ApiResponse<T> |
fail(String message,
T data)
返回执行失败的响应
|
T |
getData()
获取数据
|
String |
getMessage()
获取消息
|
long |
getStatus()
获取状态码
|
String |
getStatusText()
已过时。
|
Date |
getTimestamp()
获取时间戳
|
static <M> ApiResponse<M> |
ok()
返回执行成功的响应
|
static <T> ApiResponse<T> |
ok(T data)
返回执行成功的响应
|
void |
setData(T data)
设置数据
|
void |
setMessage(String message)
设置消息
|
void |
setStatus(long status)
设置状态码
|
void |
setStatusText(String message)
已过时。
|
void |
setTimestamp(Date timestamp)
设置时间戳
|
public static final int SUCCESS
public static final int FAIL
public static final int BUSY
public ApiResponse()
public ApiResponse(long status,
String message,
T data)
status - 状态码message - 消息data - 数据public ApiResponse(T data)
data - 数据public long getStatus()
public void setStatus(long status)
status - 状态码public String getStatusText()
getMessage()public void setStatusText(String message)
message - messagesetMessage(String)public String getMessage()
public void setMessage(String message)
message - messagepublic Date getTimestamp()
public void setTimestamp(Date timestamp)
timestamp - 时间戳public T getData()
public void setData(T data)
data - 数据public static <M> ApiResponse<M> ok()
M - 类型public static <T> ApiResponse<T> ok(T data)
T - 类型data - 返回数据public static <T> ApiResponse<T> fail(String message)
T - 类型message - 消息public static <T> ApiResponse<T> fail(String message, T data)
T - 类型message - 消息data - 数据Copyright © 2020. All rights reserved.