简体   繁体   中英

Disserializing POJO with Lombok sending complex JSON using Rest Assured

This is my first use of POJO and Lombok with Rest Assured and complex JSON.

I decided to use this approach because JSON contains many fields..

Example JSON

{"records":[{"key":"1563106","value":{"ACTION":"A"}}]}

The "value" object contains about 50 fields.

Using POJO I have created 3 classes

public class TransactionFields{
private List<RecordsItem> records;}


public class RecordsItem {
private Object value;
private String key;}


public class Value{
private String ACTION;}

By adding annotations to each of them

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor

Then I try to send a POST request using Rest Assured and do it this way:

  1. Create a Value object and add a value (I will repeat a lot more fields here)

     Value value = Value.builder().ACTION("A").build();
 RecordsItem records = RecordsItem.builder()
                  .key("1563106")
                  .value(value)
                  .build();

And I send a POST request

given()
            .contentType("application/vnd.kafka.json.v2+json")
            .body(records).
            when().post("V02")
            .then().log().all().statusCode(200);

As a result, I get the error

"error_code": 422,
"message": "Unrecognized field: value"

This is where the problems begin. I can't figure out what I'm doing wrong. and the most important question. Am I using the build pattern correctly? And is the correct object passed to POST ()?

Please help me figure it out. There are no problems with simple JSON examples, but I have not found an example with JSON that contains an array of objects.

I understood what my mistake was...

In this implementation, I needed to change the type of the value object to Value in the RecordsItem class.

public class RecordsItem {
private Value value;
private String key;

And create another object of type TransactionFields

TransactionFields transactionFields = TransactionFields.builder()
            .records(Arrays.asList(records))
            .build();

And after that use the transactionFields variable in the post request body

.body(transactionFields)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM