首页 > 解决方案 > 我可以仅使用 lombok 功能包装 dto 吗?

问题描述

我有一个用于与支付系统 api 交互的 dto 类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Builder
@Getter
@Setter
public class PaymentSystemRequestDto {
    @JsonProperty("order_id")
    protected String orderId;
    @JsonProperty("merchant_id")
    protected String merchantId;
    @JsonProperty("amount")
    protected Long amount;
    @JsonProperty("currency")
    protected String currency;
    @JsonProperty("version")
    protected String version;
    @JsonProperty("verification")
    private Boolean verification = false;
    @JsonProperty("verification_type")
    private VerificationType verificationType;

    public static class PaymentSystemRequestDtoBuilder {
        public PaymentSystemRequestDtoBuilder amount(Long amount) {
            this.amount = amount * 100;
            return this;
        }

        public PaymentSystemRequestDtoBuilder cardRegistrationPayment() {
            this.verification = true;
            this.verificationType = VerificationType.AMOUNT;
            return this;
        }
    }
}

当我发送请求时,根据我的 dto 我有这样一个 post 方法体:

{
    "order_id": "a94f1d0d-21c5-4c2d-824d-d6c1875eaa30",
    "merchant_id": "1234567",
    "amount": 100,
    "currency": "USD",
    "version": "1.0",   
}

我需要将主体包装成“请求”对象,所以主体看起来像:

{
    "request": {
        "order_id": "a94f1d0d-21c5-4c2d-824d-d6c1875eaa30",
        "merchant_id": "1234567",
        "amount": 100,
        "currency": "USD",
        "version": "1.0",   
    }
}

但不修改对象映射器序列化/反序列化功能(不使用注释 @JsonRootName

我试图用构建器创建一个内部类,但它看起来健壮而复杂......有没有办法处理这个简单的问题?

标签: javajsonlombokobjectmapper

解决方案


推荐阅读