如何使用 POJO 类使用其中字段的子集构建 json 对象

问题描述 投票:0回答:1

我需要使用 POJO 类构建一个 JSON 对象,但有时需要使用字段子集,有时需要使用所有字段来构建它。请让我知道如何在 RestAssured 中处理此问题。

rest-assured
1个回答
0
投票

Jackson 注释 @JsonInclude 可以帮助您实现这一目标。例如:

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
static class User {
    private String name;
    private String bio;
}


@Test
void name3() {
    User user1 = new User();
    user1.setName("lucas");
    RestAssured.given().log().body()
            .contentType(ContentType.JSON)
            .body(user1)
            .post("https://postman-echo.com/post");

    User user2 = new User();
    user2.setBio("nothing");
    RestAssured.given().log().body()
            .contentType(ContentType.JSON)
            .body(user2)
            .post("https://postman-echo.com/post");
}

日志:

Body:
{
    "name": "lucas"
}
Body:
{
    "bio": "nothing"
}
© www.soinside.com 2019 - 2024. All rights reserved.