[英]extract some field from rest assured response
I have a ResponseBody object and it is like that:我有一个 ResponseBody object,它是这样的:
"data": {
"id": 123,
"name": "georghe",
"surname": "sue"
}
Can i ignore id field because I want to compare it with my json data and it does not contain id field.我可以忽略 id 字段吗,因为我想将它与我的 json 数据进行比较,并且它不包含 id 字段。 I get data like that
response.getBody().path("data").toString()
but id is also coming.我得到这样的数据
response.getBody().path("data").toString()
但 id 也来了。
I tried to map to my java class like that我试图将 map 像这样
response.getBody().path("data").as(Student.java)
but I cannot use as method with path.(In my Student class, I use @jsonIgnore annotation above id)但我不能使用路径作为方法。(在我的学生 class 中,我在 id 上方使用 @jsonIgnore 注释)
I tried 2 different style but they are not working.Do you have any idea?我尝试了 2 种不同的风格,但它们都不起作用。你知道吗?
Student class:学生 class:
Student class{
private String name;
private String surname;
//getter and setters
}.
so if you want to ignore/avoid having Id in the response, @JsonIgnore is the correct approach, but make sure you're using the proper annotation:因此,如果您想忽略/避免在响应中包含 Id,@JsonIgnore 是正确的方法,但请确保您使用了正确的注释:
com.fasterxml.jackson.annotation.JsonIgnore
the same happen to me some time ago and it was caused by importing the annotation from wrong library (net.minidev.json.annotate)前段时间我也遇到了同样的情况,这是由于从错误的库中导入注释引起的(net.minidev.json.annotate)
UPDATE: so if you want to extract the Student object from data element, you can do something like this:更新:因此,如果您想从数据元素中提取学生 object,您可以执行以下操作:
final Student student = given()
.header(YOUR_HEADER, YOUR_HEADER_VALUE)
.when()
.get(YOUR_URL)
.then()
.statusCode(200)
.extract().jsonPath().getObject("data", Student.class)
You can use Mapstruct ( https://mapstruct.org/ ) with the following composition:您可以使用具有以下组成的 Mapstruct ( https://mapstruct.org/ ):
@Mapping(target = "id", ignore = true) @Mapping(目标 = "id", 忽略 = true)
(documentation, section 3.2). (文档,第 3.2 节)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.