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
    Constructor
    Description
    Result(String code, String message, T data, boolean success, Instant timestamp, String traceId)
    Creates an instance of a Result record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value of the code record component.
    Returns the value of the data record component.
    final boolean
    Indicates 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>
    fail(String code, String message)
    Create failure result 创建失败结果
    static <T> Result<T>
    fail(Throwable throwable)
    Create failure result from exception 从异常创建失败结果
    <R> Result<R>
    flatMap(Function<T, Result<R>> mapper)
    FlatMap the data 扁平映射数据
    getDataOrDefault(T defaultValue)
    Get data or default value 获取数据或默认值
    Get data or throw exception 获取数据或抛出异常
    final int
    Returns a hash code value for this object.
    boolean
    Check if failed 检查是否失败
    <R> Result<R>
    map(Function<T,R> mapper)
    Map the data to another type 将数据映射为另一种类型
    Returns the value of the message record component.
    static <T> Result<T>
    ok()
    Create success result 创建成功结果
    static <T> Result<T>
    ok(String message, T data)
    Create success result with message and data 创建带消息和数据的成功结果
    static <T> Result<T>
    ok(T data)
    Create success result with data 创建带数据的成功结果
    Execute action if failed 如果失败则执行操作
    onSuccess(Consumer<T> action)
    Execute action if success 如果成功则执行操作
    boolean
    Returns the value of the success record component.
    Returns the value of the timestamp record component.
    final String
    Returns a string representation of this record class.
    Returns the value of the traceId record component.
    withMessage(String newMessage)
    Create new result with different message 创建具有不同消息的新结果
    withTraceId(String newTraceId)
    Create new result with different trace ID 创建具有不同追踪ID的新结果

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Result

      public Result(String code, String message, T data, boolean success, Instant timestamp, String traceId)
      Creates an instance of a Result record class.
      Parameters:
      code - the value for the code record component
      message - the value for the message record component
      data - the value for the data record component
      success - the value for the success record component
      timestamp - the value for the timestamp record component
      traceId - the value for the traceId record component
  • Method Details

    • ok

      public static <T> Result<T> ok()
      Create success result 创建成功结果
      Type Parameters:
      T - the data type | 数据类型
      Returns:
      the result | 结果
    • ok

      public static <T> Result<T> ok(T data)
      Create success result with data 创建带数据的成功结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      data - the data | 数据
      Returns:
      the result | 结果
    • ok

      public static <T> Result<T> ok(String message, T data)
      Create success result with message and data 创建带消息和数据的成功结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      message - the message | 消息
      data - the data | 数据
      Returns:
      the result | 结果
    • fail

      public static <T> Result<T> fail(String code, String message)
      Create failure result 创建失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      code - the code | 代码
      message - the message | 消息
      Returns:
      the result | 结果
    • fail

      public static <T> Result<T> fail(ResultCode resultCode)
      Create failure result with result code 使用响应码创建失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      resultCode - the result code | 响应码
      Returns:
      the result | 结果
    • fail

      public static <T> Result<T> fail(ResultCode resultCode, String message)
      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

      public static <T> Result<T> fail(Throwable throwable)
      Create failure result from exception 从异常创建失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      throwable - the exception | 异常
      Returns:
      the result | 结果
    • isFailed

      public boolean isFailed()
      Check if failed 检查是否失败
      Returns:
      true if failed | 如果失败返回true
    • getDataOrThrow

      public T getDataOrThrow()
      Get data or throw exception 获取数据或抛出异常
      Returns:
      the data | 数据
      Throws:
      OpenBizException - if failed | 如果失败抛出异常
    • getDataOrDefault

      public T getDataOrDefault(T defaultValue)
      Get data or default value 获取数据或默认值
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the data or default | 数据或默认值
    • map

      public <R> Result<R> map(Function<T,R> mapper)
      Map the data to another type 将数据映射为另一种类型
      Type Parameters:
      R - the result type | 结果类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      the mapped result | 映射后的结果
    • flatMap

      public <R> Result<R> flatMap(Function<T, Result<R>> mapper)
      FlatMap the data 扁平映射数据
      Type Parameters:
      R - the result type | 结果类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      the flat mapped result | 扁平映射后的结果
    • onSuccess

      public Result<T> onSuccess(Consumer<T> action)
      Execute action if success 如果成功则执行操作
      Parameters:
      action - the action to execute | 要执行的操作
      Returns:
      this result | 此结果
    • onFailure

      public Result<T> onFailure(BiConsumer<String,String> action)
      Execute action if failed 如果失败则执行操作
      Parameters:
      action - the action to execute | 要执行的操作
      Returns:
      this result | 此结果
    • withTraceId

      public Result<T> withTraceId(String newTraceId)
      Create new result with different trace ID 创建具有不同追踪ID的新结果
      Parameters:
      newTraceId - the new trace ID | 新追踪ID
      Returns:
      the new result | 新结果
    • withMessage

      public Result<T> withMessage(String newMessage)
      Create new result with different message 创建具有不同消息的新结果
      Parameters:
      newMessage - the new message | 新消息
      Returns:
      the new result | 新结果
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • code

      public String code()
      Returns the value of the code record component.
      Returns:
      the value of the code record component
    • message

      public String message()
      Returns the value of the message record component.
      Returns:
      the value of the message record component
    • data

      public T data()
      Returns the value of the data record component.
      Returns:
      the value of the data record component
    • success

      public boolean success()
      Returns the value of the success record component.
      Returns:
      the value of the success record component
    • timestamp

      public Instant timestamp()
      Returns the value of the timestamp record component.
      Returns:
      the value of the timestamp record component
    • traceId

      public String traceId()
      Returns the value of the traceId record component.
      Returns:
      the value of the traceId record component