Loadup Common DTO
提供通用响应、分页、金额值、事件信封和通用校验,不依赖 ORM、MapStruct 或 Spring 运行库。
引入
<dependency>
<groupId>io.github.loadup-cloud</groupId>
<artifactId>loadup-commons-dto</artifactId>
</dependency>设计
内部边界、依赖与源码入口见 ARCHITECTURE.md。
接入步骤
对外契约
金额与币种
io.github.loadup.commons.money.Money 是不可变金额对象,字段为 long cent、Currency currency、String currencyValue
。cent 表示该币种的最小十进制单位,currencyValue 由 Currency 自动生成 ISO 字母代码,不能独立修改。
Money amount = Money.ofMinor(1234, CurrencyEnum.CNY);
// getCent() == 1234; getCurrencyValue() == "CNY"; toMajor() == 12.34Money 在 DTO 模块,计算及格式化在 util 模块,保持 dto 不依赖 util。
| 能力 | 契约 |
|---|---|
| Money | 不可变、有符号 long,按币种精度转换 BigDecimal,仅同币种可比较 |
| CurrencyEnum | JDK 25.0.4.1 ISO 4217 目录快照,共233项,包含历史及特殊代码 |
| 币种代码 | getCode 字母代码;getNumericCode 三位字符串,保留前导零 |
| 显示符号 | 保留指定 CNY/USD/HKD/TWD/EUR/GBP/JPY/BRL 符号;其他采用 JDK Locale.US 符号,缺失时为代码 |
| JSON | {"cent":1234,"currencyValue":"CNY"},两个必填字段,Currency 与精度不重复输出 |
JPY 使用0位,CNY/USD使用2位,KWD使用3位,CLF使用4位,不统一除以100。JDK Currency 精度定义。
特殊代码如 XAU/XXX 在枚举中保留,但没有定义最小单位,Money 拒绝使用。枚举收录不表示渠道支持或当前可交易;业务应用自行限定可用币种。升级 JDK 后应核对目录变化,历史/特殊代码数字编码可能重复,因此不提供默认按数字代码反查。
JSON cent 为整数;超出 JavaScript 安全整数范围时,前端应采用字符串 DTO/大整数处理,不能依赖普通 number 保留精度。Jackson 3 往返测试源码位于 util 模块,尚未运行。
查询、命令与分页
纯 ID 查询复用 io.github.loadup.commons.request.query.IdQuery;业务写操作采用 <业务对象><动作>Command,查询采用
<业务对象><条件>Query,分页采用 <业务对象>PageQuery。分页结果无需为各模块单独定义 DTO。
// Facade / application result
PageDTO<FileResourceDTO> page = facade.list(actor, false, null, 1, 20);
// Controller response: result + data array + pageInfo
PageResponse<FileResourceDTO> response = PageResponse.of(page);PageDTO.map(converter::toView) 支持展示 DTO 转换并保留分页元数据。PageResponse 实现 IResponse,WebMVC
不重复包裹,仍支持统一展示脱敏。非分页成功响应使用 SuccessResponse,错误响应使用 FailureResponse。
错误契约
ResultCode 与 CommonException 由 commons-error 提供。Result 是不可变响应元数据,失败工厂只接收枚举 ResultCode。FailureResponse.of (code) 或 of (code, traceId) 不接收字符串错误码/文案;读取外部报文仍支持 Jackson creator。
Unified pagination contract
Pure pagination inputs reuse io.github.loadup.commons.dto.PageQuery. HTTP fields remain page and size, with
omitted/null values defaulting to 1 and 20; explicit invalid bounds are rejected and size is capped at 100. Business
queries retain their filter fields and flat pagination fields but delegate policy to PageQuery.normalized, preserving
their declared module error code. Mutable UPMS queries expose pagination() for validation at the service boundary.
Sort whitelists remain business-specific.
Domain Gateway results use io.github.loadup.commons.domain.PageResult<T>, shared with UPMS. Seven duplicate business
Page wrappers have been removed. Application mapping uses the injected Spring MapStruct item converter and
PageResult.map; PageDTO.of(result) adapts the unchanged total/page/size metadata. Facades return PageDTO, and
Controllers return PageResponse. No HTTP envelope enters a Gateway.
The response remains {result, data: [...], pageInfo: {totalCount, pageIndex, pageSize, ...}}; data is the current-page
array. SuccessResponse serves ordinary success data only; its duplicate pagination field and ofPage factories have been
removed. Empty PageDTO defaults to an empty array and metadata 0/1/20. Existing consumer code referencing removed
business Page wrappers, DictionaryTypePageQuery, AccountLoginPageQuery or SuccessResponse.ofPage must use the shared
contracts. No URL or frontend payload-field migration is required.
职责迁移
持久化基类使用 io.github.loadup.components.database.dataobject.BaseDO,来自 loadup-components-database。
共享映射配置使用 io.github.loadup.components.runtime.mapping.LoadUpMapStructConfig,来自 loadup-components-runtime。
渠道基础词汇和 ChannelAmounts.cnyAmount 来自 loadup-pay-payment-channel-contracts;商业合约引用来自 Pay
的 pay-contracts-commercial。消费方按实际源码直接声明依赖,不通过 DTO 隐式引入这些能力。
设计与实现
职责与边界
提供通用响应、分页、金额值、事件信封和通用校验,不依赖 ORM、MapStruct 或 Spring 运行库。
Maven 依赖边界
直接依赖的外部坐标(不含测试与 provided scope):
com.fasterxml.jackson.core:jackson-annotationsio.swagger.core.v3:swagger-annotations
实现入口
主要源码入口:
分层与调用路径
业务模型 → Money / PageResult / Result / PageDTO → 上层组件扩展契约
DTO:由实现方或调用方按接口定义对接。ResultCode:由实现方或调用方按接口定义对接。IEnum:由实现方或调用方按接口定义对接。CommonConstants:由实现方或调用方按接口定义对接。AssertionCallback:由实现方或调用方按接口定义对接。
集成方式与配置示例见 README.md。
Money 与 CurrencyEnum
Money 作为基础值对象位于 dto,只有 JDK 类型及既有 Jackson annotations 依赖。三个字段 final,currencyValue 在构造时派生,不提供 setter。支持负值表达差额/冲正,不在通用值对象中施加支付金额必须为正的业务限制。
Jackson properties creator 使用 Long 检查缺失/空金额;对外只输出 cent 和 currencyValue,Currency 与精度为派生内部属性。没有默认币种、汇率换算或隐式浮点入口。
CurrencyEnum 固定字母代码、三位数字代码和显示符号,来自 JDK 25.0.4.1 的233项目录,包含历史代码;toCurrency 和精度读取委托 JDK。币种目录是版本快照,跨实例应采用一致 JDK 币种数据;历史币种精度变化需要业务迁移评估。市场有效性、渠道支持和现金舍入规则由业务定义。
MoneyFormatter/MoneyUtil 在 util,单向依赖此模块。数学运算、Jackson 往返与枚举完整性测试集中在 util,尚未编译或执行。
分页职责
PageDTO 是 Facade 的通用分页数据,不携带成功状态。PageResponse 是 HTTP 成功分页 envelope,实现 sealed IResponse,与 SuccessResponse/FailureResponse 并列,字段顺序为 result、data、pageInfo。领域层保留自己的分页对象,app 的 Spring MapStruct Converter 映射记录和页码元数据;Web 层只包装响应或通过 PageDTO.map 做展示投影。
该划分减少七个业务模块的重复分页 DTO;分页 HTTP data 是列表,不再次嵌套 items/total。商户和合约前端已改用顶层 pageInfo.totalCount。字典命令合并原有 Request 包装,直接携带 id/typeCode;Router.http 和前端请求同步改为扁平字段。
错误契约
ResultCode 与 CommonException 由 commons-error 提供。Result 是不可变响应元数据,失败工厂只接收枚举 ResultCode。FailureResponse.of (code) 或 of (code, traceId) 不接收字符串错误码/文案;读取外部报文仍支持 Jackson creator。
Unified pagination contract
Pure pagination inputs reuse io.github.loadup.commons.dto.PageQuery. HTTP fields remain page and size, with
omitted/null values defaulting to 1 and 20; explicit invalid bounds are rejected and size is capped at 100. Business
queries retain their filter fields and flat pagination fields but delegate policy to PageQuery.normalized, preserving
their declared module error code. Mutable UPMS queries expose pagination() for validation at the service boundary.
Sort whitelists remain business-specific.
Domain Gateway results use io.github.loadup.commons.domain.PageResult<T>, shared with UPMS. Seven duplicate business
Page wrappers have been removed. Application mapping uses the injected Spring MapStruct item converter and
PageResult.map; PageDTO.of(result) adapts the unchanged total/page/size metadata. Facades return PageDTO, and
Controllers return PageResponse. No HTTP envelope enters a Gateway.
The response remains {result, data: [...], pageInfo: {totalCount, pageIndex, pageSize, ...}}; data is the current-page
array. SuccessResponse serves ordinary success data only; its duplicate pagination field and ofPage factories have been
removed. Empty PageDTO defaults to an empty array and metadata 0/1/20. Existing consumer code referencing removed
business Page wrappers, DictionaryTypePageQuery, AccountLoginPageQuery or SuccessResponse.ofPage must use the shared
contracts. No URL or frontend payload-field migration is required.
职责迁移
持久化基类使用 io.github.loadup.components.database.dataobject.BaseDO,来自 loadup-components-database。
共享映射配置使用 io.github.loadup.components.runtime.mapping.LoadUpMapStructConfig,来自 loadup-components-runtime。
渠道基础词汇和 ChannelAmounts.cnyAmount 来自 loadup-pay-payment-channel-contracts;商业合约引用来自 Pay
的 pay-contracts-commercial。消费方按实际源码直接声明依赖,不通过 DTO 隐式引入这些能力。