简体   繁体   English

从 rest 中提取一些字段保证响应

[英]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.

相关问题 放心:从响应列表中提取值 - Rest Assured: extract value from Response List 如何验证响应并从 Rest Assured 中的响应正文中提取值? - How to validate a response and extract a value from Response body in Rest Assured? 放心。 是否可以从响应 json 中提取 JSONObject/JSONArray? - Rest-assured. Is it possible to extract JSONObject/JSONArray from response json? 如何使用通用代码从 REST Assured 响应中提取 cookie - How to extract cookies from a REST Assured response using common code 从Rest Assured Json响应中获取多个项目的随机字段 - Get random field from Rest Assured Json response with multiple items 如何使用放心从具有多个命名空间的 SOAP XML 响应中提取价值? - How to extract value from SOAP XML response with multiple namespaces using Rest-assured? 在REST Assured中,如何检查响应中是否存在字段? - In REST Assured, how can I check if a field is present or not in the response? 放心 - 如何验证具有相同名称的 JSON 响应字段 - Rest assured - How to validate JSON response field with the same name 如何将访问令牌值从一个 API 响应主体(在一个类中)提取到另一个 API 标头(在另一个类中)在 rest 保证代码中 - How to extract accesss token value from one API response body(in one class) into another API header(in another class) in rest assured code 如何从嵌套列表中提取项目 - Rest Assured - How to extract item from nested list - Rest Assured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM