package com.lianlu.aiboot.domain.result;
import java.io.Serializable;
/**
* 统一封装http返回值处理
* @param <T>
*/
返回一个字符串数据
//HttpResult<String> resultStr = new HttpResult<>(200, "OK", "Hello, World!");
//
返回一个对象列表数据
//HttpResult<List<User>> resultList = new HttpResult<>(200, "Users fetched successfully", userList);
public class HttpResult<T> implements Serializable {
private static final long serialVersionUID = 1L;
// HTTP状态码
private int code;
// 提示信息
private String message;
// 数据部分,使用泛型T表示任意类型
private T data;
// 无参构造函数
public HttpResult() {
}
// 带所有参数的构造函数
public HttpResult(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
// Getter和Setter方法
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
// 重写toString方法,方便打印和查看
@Override
public String toString() {
return "HttpResult{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
'}';
}
}
《封装Http风格返回值》java-web
最新推荐文章于 2024-11-15 15:15:21 发布