【问题标题】:How to Order Json while deserialization using annotation?如何在使用注释反序列化时订购 Json?
【发布时间】:2019-09-05 14:04:06
【问题描述】:

我正在使用以下请求负载:

{
    "accountId": "8b7d80bd-120e-4059-9802-a8af9ac04038",
    "name": "Client sucqtixp",
    "email": "Niles@qa4life.com",
    "phone": "1234567890",
    "frequency": "MONTHLY"
    "paymentMethod": {
        "id": "00eef328-bd2c-4ccb-8b8e-12bd0c2552ad",
        "type": "BANK_ACCOUNT"
    }
}

我正在使用@RequestBody

@Data
@JsonInclude( JsonInclude.Include.NON_NULL )
public class AccountVO
{
    private UUID accountId;

    private String name;

    private String email;

    private String phone;

    private String frequency;

    private PaymentMethodVO paymentMethod;

    public void setPaymentMethod( PaymentMethodVO paymentMethod )
    {
        paymentMethod.setSevaluation( paymentMethod.getSevaluation() == null ? Frequency.valueOf( this.sevaluation ) : paymentMethod.getSevaluation() );
        this.paymentMethod = paymentMethod;
    }
}

如果未提供,我正在尝试将帐户频率设置为 paymentMethod 的频率,但是当在 json 请求 frequencypaymentMethod 之后发送时,paymentMethod 的频率为 null。

如果 json 请求以任何顺序出现,我希望它会做同样的事情。

我正在使用弹簧靴和com.fasterxml.jackson.annotation

【问题讨论】:

    标签: java json spring-boot jackson gson


    【解决方案1】:

    如果我了解您的问题,您需要在创建AccountVO 的实例时对您的属性进行一些处理。

    所以你可以在构造函数中使用@JsonCreator

    @Data
    @JsonInclude(Include.NON_NULL)
    public class AccountVO {
    
        // Fields omitted
    
        public AccountVO(@JsonProperty("accountId") String accountId,
                         @JsonProperty("name") String name,
                         @JsonProperty("email") String email,
                         @JsonProperty("phone") String phone,
                         @JsonProperty("frequency") String frequency,
                         @JsonProperty("paymentMethod") PaymentMethodVO paymentMethod) {
    
            this.accountId = accountId;
            this.name = name;
            this.email = email;
            this.phone = phone;
            this.frequency = frequency;
            this.paymentMethod = paymentMethod; // Do any other processing here
        }
    }
    

    或者,您可以在工厂方法中使用@JsonCreator

    @Data
    @JsonInclude(Include.NON_NULL)
    public class AccountVO {
    
        // Fields omitted
    
        @JsonCreator
        public static AccountVO factory(
                         @JsonProperty("accountId") String accountId,
                         @JsonProperty("name") String name,
                         @JsonProperty("email") String email,
                         @JsonProperty("phone") String phone,
                         @JsonProperty("frequency") String frequency,
                         @JsonProperty("paymentMethod") PaymentMethodVO paymentMethod) {
    
            AccountVO account = new AccountVO();
            account.setAccountId(accountId);
            account.setName(name);
            account.setEmail(email);
            account.setPhone(phone);
            account.setFrequency(frequency);
            account.setPaymentMethod(paymentMethod); // Do any other processing here
            return account;
        }
    

    【讨论】:

    • 我使用了@JsonPropertyOrder({ "accountId", "name", "email", "phone", "frequency", "paymentMethod" }),但仍然遇到同样的问题。有没有其他注释可以用于订单?
    • 当我使用@JsonPropertyOrder 并在setPaymentMethod 中调试时,this.sevaluationnull 但我在paymentMethod 之前传递了sevaluation 的值。
    • @ankit 你可以试试@JsonCreator。让我相应地更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2019-10-19
    • 2014-07-01
    • 1970-01-01
    • 2022-01-13
    • 2020-07-26
    相关资源
    最近更新 更多