如何在RestAssured中发送指定JSON格式的POST请求?
用RestAssured发送指定JSON的POST请求
1. 引入RestAssured依赖(Maven)
如果用Maven管理项目,先在pom.xml中添加依赖:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency>
2. 方法一:直接传入JSON字符串
把你提供的JSON作为字符串传入请求体,Java 15+支持文本块,无需转义引号:
import static io.restassured.RestAssured.given; import static io.restassured.http.ContentType.JSON; public class PostRequestExample { public static void main(String[] args) { String requestBody = """ { "customer_id":"001", "appointment_id":"001", "appointment_time":"2022-10-04 00:00:00", "submitted_time":"2022-10-04 00:00:00", "first_name":"ethinic", "last_name":"one", "date_of_birth":"1910-10-10", "email_address":"test@gmail.com", "address_1":"24", "address_2":"testtwo", "postcode":"abc123", "phone":"1234567890", "questions":[ { "ethnicity":"Other Ethnic Groups - Chinese" }, { "do_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19":"No" }, { "what_is_the_main_reason_for_your_appointment_with_us":"Eye health concern" } ] } """; given() .contentType(JSON) .body(requestBody) .when() .post("https://your-api-endpoint.com/appointments") // 替换为实际接口地址 .then() .statusCode(200); // 验证响应状态码 } }
3. 方法二:用HashMap构建请求体
如果需要动态修改字段值,用HashMap和List构建请求体更灵活:
import static io.restassured.RestAssured.given; import static io.restassured.http.ContentType.JSON; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class PostRequestWithMap { public static void main(String[] args) { // 构建questions列表 Map<String, String> question1 = new HashMap<>(); question1.put("ethnicity", "Other Ethnic Groups - Chinese"); Map<String, String> question2 = new HashMap<>(); question2.put("do_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19", "No"); Map<String, String> question3 = new HashMap<>(); question3.put("what_is_the_main_reason_for_your_appointment_with_us", "Eye health concern"); List<Map<String, String>> questions = new ArrayList<>(); questions.add(question1); questions.add(question2); questions.add(question3); // 构建主请求体 Map<String, Object> requestBody = new HashMap<>(); requestBody.put("customer_id", "001"); requestBody.put("appointment_id", "001"); requestBody.put("appointment_time", "2022-10-04 00:00:00"); requestBody.put("submitted_time", "2022-10-04 00:00:00"); requestBody.put("first_name", "ethinic"); requestBody.put("last_name", "one"); requestBody.put("date_of_birth", "1910-10-10"); requestBody.put("email_address", "test@gmail.com"); requestBody.put("address_1", "24"); requestBody.put("address_2", "testtwo"); requestBody.put("postcode", "abc123"); requestBody.put("phone", "1234567890"); requestBody.put("questions", questions); given() .contentType(JSON) .body(requestBody) .when() .post("https://your-api-endpoint.com/appointments") .then() .statusCode(200); } }
4. 方法三:通过POJO类构建请求体
对于复杂且固定的JSON结构,创建POJO类更规范,便于维护:
定义Question类
public class Question { private String ethnicity; private String do_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19; private String what_is_the_main_reason_for_your_appointment_with_us; // 生成getter、setter方法 public String getEthnicity() { return ethnicity; } public void setEthnicity(String ethnicity) { this.ethnicity = ethnicity; } public String getDo_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19() { return do_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19; } public void setDo_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19(String value) { this.do_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19 = value; } public String getWhat_is_the_main_reason_for_your_appointment_with_us() { return what_is_the_main_reason_for_your_appointment_with_us; } public void setWhat_is_the_main_reason_for_your_appointment_with_us(String value) { this.what_is_the_main_reason_for_your_appointment_with_us = value; } }
定义Customer类
import java.util.List; public class Customer { private String customer_id; private String appointment_id; private String appointment_time; private String submitted_time; private String first_name; private String last_name; private String date_of_birth; private String email_address; private String address_1; private String address_2; private String postcode; private String phone; private List<Question> questions; // 生成getter、setter方法 public String getCustomer_id() { return customer_id; } public void setCustomer_id(String customer_id) { this.customer_id = customer_id; } public String getAppointment_id() { return appointment_id; } public void setAppointment_id(String appointment_id) { this.appointment_id = appointment_id; } public String getAppointment_time() { return appointment_time; } public void setAppointment_time(String appointment_time) { this.appointment_time = appointment_time; } public String getSubmitted_time() { return submitted_time; } public void setSubmitted_time(String submitted_time) { this.submitted_time = submitted_time; } public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public String getDate_of_birth() { return date_of_birth; } public void setDate_of_birth(String date_of_birth) { this.date_of_birth = date_of_birth; } public String getEmail_address() { return email_address; } public void setEmail_address(String email_address) { this.email_address = email_address; } public String getAddress_1() { return address_1; } public void setAddress_1(String address_1) { this.address_1 = address_1; } public String getAddress_2() { return address_2; } public void setAddress_2(String address_2) { this.address_2 = address_2; } public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public List<Question> getQuestions() { return questions; } public void setQuestions(List<Question> questions) { this.questions = questions; } }
发送请求代码
import static io.restassured.RestAssured.given; import static io.restassured.http.ContentType.JSON; import java.util.ArrayList; import java.util.List; public class PostRequestWithPOJO { public static void main(String[] args) { // 创建Question实例 Question question1 = new Question(); question1.setEthnicity("Other Ethnic Groups - Chinese"); Question question2 = new Question(); question2.setDo_you_or_anyone_in_your_household_have_a_fever_a_new_persistent_cough_loss_of_taste_or_smell_or_any_other_symptoms_of_covid_19("No"); Question question3 = new Question(); question3.setWhat_is_the_main_reason_for_your_appointment_with_us("Eye health concern"); List<Question> questions = new ArrayList<>(); questions.add(question1); questions.add(question2); questions.add(question3); // 创建Customer实例 Customer customer = new Customer(); customer.setCustomer_id("001"); customer.setAppointment_id("001"); customer.setAppointment_time("2022-10-04 00:00:00"); customer.setSubmitted_time("2022-10-04 00:00:00"); customer.setFirst_name("ethinic"); customer.setLast_name("one"); customer.setDate_of_birth("1910-10-10"); customer.setEmail_address("test@gmail.com"); customer.setAddress_1("24"); customer.setAddress_2("testtwo"); customer.setPostcode("abc123"); customer.setPhone("1234567890"); customer.setQuestions(questions); given() .contentType(JSON) .body(customer) .when() .post("https://your-api-endpoint.com/appointments") .then() .statusCode(200); } }
内容的提问来源于stack exchange,提问作者fareen taj
相关产品推荐
相关产品推荐

