Record Class Result<T>
java.lang.Object
java.lang.Record
cloud.opencode.base.web.Result<T>
- Type Parameters:
T- the data type | 数据类型- Record Components:
code- the result code | 响应码message- the result message | 响应消息data- the result data | 响应数据success- whether the operation was successful | 操作是否成功timestamp- the response timestamp | 响应时间戳traceId- the trace ID for request tracking | 请求追踪ID
public record Result<T>(String code, String message, T data, boolean success, Instant timestamp, String traceId)
extends Record
Result
统一响应结果
Standard API response wrapper with trace support.
带追踪支持的标准API响应包装器。
Features | 主要功能:
- Immutable record - 不可变记录
- Trace ID support - 追踪ID支持
- Fluent API - 流式API
- Functional operations - 函数式操作
Usage Examples | 使用示例:
Result<User> ok = Result.ok(user);
Result<?> fail = Result.fail("E001", "Not found");
T data = result.getDataOrThrow();
Result<DTO> mapped = result.map(user -> toDTO(user));
Security | 安全性:
- Thread-safe: Yes (immutable record) - 是(不可变记录)
- Null-safe: Yes (data can be null) - 是(数据可以为null)
- Since:
- JDK 25, opencode-base-web V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncode()Returns the value of thecoderecord component.data()Returns the value of thedatarecord component.final booleanIndicates whether some other object is "equal to" this one.static <T> Result<T> fail(ResultCode resultCode) Create failure result with result code 使用响应码创建失败结果static <T> Result<T> fail(ResultCode resultCode, String message) Create failure result with result code and custom message 使用响应码和自定义消息创建失败结果static <T> Result<T> Create failure result 创建失败结果static <T> Result<T> Create failure result from exception 从异常创建失败结果<R> Result<R> FlatMap the data 扁平映射数据getDataOrDefault(T defaultValue) Get data or default value 获取数据或默认值Get data or throw exception 获取数据或抛出异常final inthashCode()Returns a hash code value for this object.booleanisFailed()Check if failed 检查是否失败<R> Result<R> Map the data to another type 将数据映射为另一种类型message()Returns the value of themessagerecord component.static <T> Result<T> ok()Create success result 创建成功结果static <T> Result<T> Create success result with message and data 创建带消息和数据的成功结果static <T> Result<T> ok(T data) Create success result with data 创建带数据的成功结果onFailure(BiConsumer<String, String> action) Execute action if failed 如果失败则执行操作Execute action if success 如果成功则执行操作booleansuccess()Returns the value of thesuccessrecord component.Returns the value of thetimestamprecord component.final StringtoString()Returns a string representation of this record class.traceId()Returns the value of thetraceIdrecord component.withMessage(String newMessage) Create new result with different message 创建具有不同消息的新结果withTraceId(String newTraceId) Create new result with different trace ID 创建具有不同追踪ID的新结果
-
Constructor Details
-
Result
public Result(String code, String message, T data, boolean success, Instant timestamp, String traceId) Creates an instance of aResultrecord class.- Parameters:
code- the value for thecoderecord componentmessage- the value for themessagerecord componentdata- the value for thedatarecord componentsuccess- the value for thesuccessrecord componenttimestamp- the value for thetimestamprecord componenttraceId- the value for thetraceIdrecord component
-
-
Method Details
-
ok
Create success result 创建成功结果- Type Parameters:
T- the data type | 数据类型- Returns:
- the result | 结果
-
ok
Create success result with data 创建带数据的成功结果- Type Parameters:
T- the data type | 数据类型- Parameters:
data- the data | 数据- Returns:
- the result | 结果
-
ok
-
fail
-
fail
Create failure result with result code 使用响应码创建失败结果- Type Parameters:
T- the data type | 数据类型- Parameters:
resultCode- the result code | 响应码- Returns:
- the result | 结果
-
fail
Create failure result with result code and custom message 使用响应码和自定义消息创建失败结果- Type Parameters:
T- the data type | 数据类型- Parameters:
resultCode- the result code | 响应码message- the custom message | 自定义消息- Returns:
- the result | 结果
-
fail
-
isFailed
public boolean isFailed()Check if failed 检查是否失败- Returns:
- true if failed | 如果失败返回true
-
getDataOrThrow
Get data or throw exception 获取数据或抛出异常- Returns:
- the data | 数据
- Throws:
OpenBizException- if failed | 如果失败抛出异常
-
getDataOrDefault
-
map
-
flatMap
-
onSuccess
-
onFailure
Execute action if failed 如果失败则执行操作- Parameters:
action- the action to execute | 要执行的操作- Returns:
- this result | 此结果
-
withTraceId
-
withMessage
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
code
-
message
-
data
-
success
-
timestamp
-
traceId
-