简体   繁体   中英

extract some field from rest assured response

I have a ResponseBody object and it is like that:

"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. I get data like that response.getBody().path("data").toString() but id is also coming.

I tried to map to my java class like that

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)

I tried 2 different style but they are not working.Do you have any idea?

Student 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:

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)

UPDATE: so if you want to extract the Student object from data element, you can do something like this:

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:

@Mapping(target = "id", ignore = true)

(documentation, section 3.2).

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