'Generate json request body using Lombok annotation

I tried multiple pattern , but still no success how can I create JSON Array object with nested JSON object similar like below

{
  "deduction": [
    {
      "id": "50258779",
      "amount": {
        "value": 13.24,
        "currency": "INR"
      },
      "transfer": "DEPOSIT",
      "fund": "RL",
      "description": "TD description",
      "code": "codeNumber"
    },
    {
      "id": "50258779",
      "amount": {
        "value": 13.24,
        "currency": "INR"
      },
      "transfer": "DEPOSIT",
      "fund": "RL",
      "description": "TD description",
      "code": "codeNumber"
    }
  ]
}

I had generated Class to build this request schema :

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;


@Builder
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Transf{

    private List<Deduction> deduction;

    @Builder
    @Getter
    @Setter
    public class Deduction {

        private Amount amount;
        private String transfer;
        private String code;
        private String fund;
        private String description;
        private Integer id;

    }

    @Builder
    @Getter
    @Setter
    public class Amount {

        private String currency;
        private Double value;

    }
}

Trying to create json request body to be send using rest assured

public Transf trans()
    {
        return Transf.builder()
            .deduction(Transf.Deduction.builder().transfer("")).build();
    }

But getting syntax error on this , need to know how can I make this work



Solution 1:[1]

First, you define private List<Deduction> deduction; --> you will supply List.

Second, to make builder work, you have to call .build()

public Transf trans() {
    return Transf.builder()
            .deduction(Arrays.asList(Transf.Deduction.builder().transfer("").build()))
            .build();
}

Solution 2:[2]

Instead of

Transf.Deduction.builder().transfer("")

you need to have

Transf.Deduction.builder().transfer("").build()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lucas-nguyen-17
Solution 2 Nikolai Shevchenko