java - 为数组类型设置数据 - 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 个字符串变量。
提前感谢您的建议。
解决方案
您不必为类型创建单独的类,因为它是一个列表,只需将其声明为字符串列表
@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);
推荐阅读
- java - Gradle SubProject 任务没有被拾取
- javascript - 我想在放大的图像中添加下一个和上一个按钮
- javascript - Firebase:如何使用变量引用 Firebase 中的路径
- sql - 如何为以下问题做出选择语句?
- vsto - VSTO 功能区命令重新启动 Office.js 加载项
- dataframe - 仅在数据框中存在列时应用条件
- php - 如何使用 php 将 API 中的数据图像保存在服务器上?
- asp.net - 将 varchar 转换为 datetime 数据类型
- node.js - 如何从 Sendgrid 响应中获取嵌入动态参数的渲染模板?
- html - 当通过 Ctrl+A 清除值并且上一个值与假设为新值相同时,角度输入框值未从 ngModelChange 事件更新