尝试使用POJO和lombok构建器构建具有保证保证的请求正文
我已经创建了POJO对象,不确定如何为下面的代码构建请求正文
{
"con": {
"ipAdr": "1.11.222",
"site": "ghyt"
},
"cred": {
"login": {
"user": "abc",
"pd": "xyz"
}
},
"view": "qwe"
}
答案 0 :(得分:0)
首先,您需要将首选的JSON序列化器添加到classpath或作为Maven / Gradle项目中的依赖项。从RestAssured 4.0.0开始,您可以尝试:
这是带有lombok的@Builder注释的POJO的示例:
@Data
@Builder
public class MyPojo {
private String view;
}
和RestAssured发布请求:
MyPojo pojo = MyPojo.builder()
.view("some-value")
.build();
RestAssured.given()
.contentType(ContentType.JSON)
.body(pojo)
.post("http://www.example.com");
RestAssured文档页面上有很多有关此主题的信息-https://github.com/rest-assured/rest-assured/wiki/Usage#object-mapping
答案 1 :(得分:0)
与您的json等效的是:
public class Con {
public String ipAdr;
public String site;
}
-----------------------------------com.example.Cred.java-----------------------------------
package com.example;
@Data
public class Cred {
public Login login;
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
@Data
public class Example {
public Con con;
public Cred cred;
public String view;
}
-----------------------------------com.example.Login.java-----------------------------------
package com.example;
@Data
public class Login {
public String user;
public String pd;
enter code here
} ```