首页 > 解决方案 > 为数组类型设置数据 - RestAssured Post 方法

问题描述

我正在尝试为这个机构创建一个负载并将做一个 POST。

{
    "location": {
        "lat": -38.383494,
        "lng": 33.427362
    },
    "accuracy": 50,
    "name": "Frontline house Seattle",
    "phone_number": "(1) 952 893 3937",
    "address": "29, side layout, cohen 09",
    "types": [
        "shoe park",
        "shop"
    ],
    "website": "http://google.com",
    "language": "French-EN"
}

这些是我创建的 3 个类

@Getter
@Setter
public class Payload {
    private Location location;
    private int accuracy;
    private String name;
    private String phone_number;
    private String address;
    private List<Types> types;
    private String website;
    private String language;
}

@Getter
@Setter
public class Location {
    private double lat;
    private double lng;
}

@Setter
@Getter
public class Types {
    private String zero;
    private String one;
}

下面将用于发布帖子

 @Test
    public void createAddress(){
        Location location = new Location(); // Location Class
        location.setLat(-38.383494);
        location.setLng(33.427362);

       Types[] type = new Types[2]; // Type Class


        Payload payload = new Payload();
        payload.setLocation(location); // This is how we will set the Location type class in Payload
        payload.setAccuracy(50);
        payload.setName("Frontline house");
        payload.setPhone_number("(1) 952 893 3937");
        payload.setAddress("29, side layout, cohen 09");
        payload.setTypes();

}

问题: 我不确定如何为 Types 设置值来创建此有效负载。我也有一种感觉我错误地声明了 Types。在那个类中,我声明了 2 个字符串变量。

提前感谢您的建议。

标签: javapojorest-assured

解决方案


您不必为类型创建单独的类,因为它是一个列表,只需将其声明为字符串列表

@Setter @Getter private List<String> types;

这些的 getter 和 setter 将是

public List<String> getTypes() { return types; } public void setTypes(List<String> types) { this.types = types; }

在你的测试中

List<String> myList =new ArrayList<String>();
    myList.add("shoe park");
    myList.add("shop");

    payload.setTypes(myList);

推荐阅读