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