我正在尝试为这个机构创建一个付费负载,并将进行 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();
}
问题:我不确定如何为类型设置值以创建此有效负载。我也有一种感觉,我错误地声明了类型。在该类中,我声明了 2 个字符串变量。
提前感谢您的建议。
您不必为类型创建单独的类,因为它是一个列表,只需将其声明为字符串列表即可
@Setter
@Getter
private List<String> types;
这些的获取者和二传手将是
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);