You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TestNG Java在RestAssured中构建指定JSON请求体的POJO修正问题

解决方案

直接修正你的POJO结构,让callbackURLs对应一个包含两个字符串数组的对象,而非包含单个URL对象的数组。以下是正确的实现步骤:


1. 定义CallbackURLs类

这个类对应JSON中的callbackURLs对象,包含两个数组属性:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public class CallbackURLs {
    @JsonProperty("identityCallbackUrls")
    private List<String> identityCallbackUrls;
    
    @JsonProperty("notificationUrls")
    private List<String> notificationUrls;

    // Getters & Setters
    public List<String> getIdentityCallbackUrls() {
        return identityCallbackUrls;
    }

    public void setIdentityCallbackUrls(List<String> identityCallbackUrls) {
        this.identityCallbackUrls = identityCallbackUrls;
    }

    public List<String> getNotificationUrls() {
        return notificationUrls;
    }

    public void setNotificationUrls(List<String> notificationUrls) {
        this.notificationUrls = notificationUrls;
    }
}

2. 定义主请求类(如EmployeeRequest)

包含所有顶层字段,以及callbackURLs属性:

import com.fasterxml.jackson.annotation.JsonProperty;

public class EmployeeRequest {
    @JsonProperty("employerId")
    private String employerId;
    
    @JsonProperty("firstName")
    private String firstName;
    
    @JsonProperty("lastName")
    private String lastName;
    
    @JsonProperty("last4TIN")
    private String last4TIN;
    
    @JsonProperty("emailId")
    private String emailId;
    
    @JsonProperty("cellPhone")
    private String cellPhone;
    
    @JsonProperty("callbackURLs")
    private CallbackURLs callbackURLs;
    
    @JsonProperty("addressLine1")
    private String addressLine1;
    
    @JsonProperty("addressLine2")
    private String addressLine2;
    
    @JsonProperty("zip")
    private String zip;
    
    @JsonProperty("city")
    private String city;
    
    @JsonProperty("state")
    private String state;
    
    @JsonProperty("dateOfBirth")
    private String dateOfBirth;

    // 所有字段的Getters & Setters
    public String getEmployerId() { return employerId; }
    public void setEmployerId(String employerId) { this.employerId = employerId; }
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
    public String getLast4TIN() { return last4TIN; }
    public void setLast4TIN(String last4TIN) { this.last4TIN = last4TIN; }
    public String getEmailId() { return emailId; }
    public void setEmailId(String emailId) { this.emailId = emailId; }
    public String getCellPhone() { return cellPhone; }
    public void setCellPhone(String cellPhone) { this.cellPhone = cellPhone; }
    public CallbackURLs getCallbackURLs() { return callbackURLs; }
    public void setCallbackURLs(CallbackURLs callbackURLs) { this.callbackURLs = callbackURLs; }
    public String getAddressLine1() { return addressLine1; }
    public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; }
    public String getAddressLine2() { return addressLine2; }
    public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; }
    public String getZip() { return zip; }
    public void setZip(String zip) { this.zip = zip; }
    public String getCity() { return city; }
    public void setCity(String city) { this.city = city; }
    public String getState() { return state; }
    public void setState(String state) { this.state = state; }
    public String getDateOfBirth() { return dateOfBirth; }
    public void setDateOfBirth(String dateOfBirth) { this.dateOfBirth = dateOfBirth; }
}

3. 在TestNG测试中构建并发送请求

使用RestAssured将POJO序列化为JSON:

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.testng.annotations.Test;
import java.util.Arrays;

public class EmployeeCreationTest {
    @Test
    public void sendEmployeeRequest() {
        // 构建CallbackURLs实例
        CallbackURLs callbackURLs = new CallbackURLs();
        callbackURLs.setIdentityCallbackUrls(Arrays.asList("https://webhook.site/4d0e80d0-ece1-4208-8f76-9ac7998c7f8a"));
        callbackURLs.setNotificationUrls(Arrays.asList("https://webhook.site/4d0e80d0-ece1-4208-8f76-9ac7998c7f8a"));

        // 构建主请求实例
        EmployeeRequest request = new EmployeeRequest();
        request.setEmployerId("{{employerId}}");
        request.setFirstName("Sarah");
        request.setLastName("Longfield");
        request.setLast4TIN("7066");
        request.setEmailId("deepakre+356441@gmail.com.com");
        request.setCellPhone("+912637489264");
        request.setCallbackURLs(callbackURLs);
        request.setAddressLine1("250 Vesey Street");
        request.setAddressLine2("");
        request.setZip("10281");
        request.setCity("New York");
        request.setState("NY");
        request.setDateOfBirth("09-12-1995");

        // 发送POST请求
        RestAssured.given()
                .contentType(ContentType.JSON)
                .body(request)
                .when()
                .post("your-target-api-endpoint")
                .then()
                .statusCode(200); // 根据实际接口调整预期状态码
    }
}

核心修正说明

  • 把原来的List类型callbacks字段,替换为单个CallbackURLs对象,内部包含两个List<String>属性,对应JSON中的两个数组。
  • 使用@JsonProperty注解确保POJO属性名和JSON字段名完全匹配,避免序列化时出现命名偏差(比如大小写、字段名不一致)。

内容的提问来源于stack exchange,提问作者Deepak Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 16:16:06