ankit
ankit

Reputation: 2845

How to Order Json while deserialization using annotation?

I am using following request payload:

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

I am using @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;
    }
}

I am trying to set frequency of account to paymentMethod's frequency if not provided but when in json request frequency send after paymentMethod, then null comes in frequency of paymentMethod.

I want if json request come in any order it will do same.

I am using spring boot and com.fasterxml.jackson.annotation.

Upvotes: 0

Views: 406

Answers (1)

cassiomolin
cassiomolin

Reputation: 131147

If I understand your issue, you need to perform some processing on your properties when creating an instance of AccountVO.

So you could use @JsonCreator in a constructor:

@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
    }
}

Alternatively, you could use @JsonCreator in a factory method:

@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;
    }

Upvotes: 1

Related Questions