英文:
Is there a way to pass an API ArrayList field in POJO builder object?
问题
我有这个包含一些嵌套字段的API请求负载:
{
"myId": "studentOne",
"myFirstName": "joe",
"myLastName": "bloggs",
"demoPackages": [
{
"myparts": "https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l",
"myPackages": [
"https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1"
]
}
]
}
我有对应的请求模型DTO:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoRequest {
private String myId;
private String myFirstName;
private String myLastName;
private ArrayList<DemoPackage> demoPackages;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoPackage {
private String myparts;
private ArrayList<String> myPackages;
}
}
现在的挑战是,当创建一个持有API请求的构建器对象时,我不知道如何传递ArrayList字段。我尝试了这样做:
public Object createMyPayload(String myId, String myFirstName, String myLastName, ArrayList<String> myparts, ArrayList<String> myPackages) {
return DemoRequest.builder()
.myId(myId)
.myFirstName(myFirstName)
.myLastName(myLastName)
.demoPackages(Arrays.asList(DemoPackage.builder().myparts(myparts).myPackages(myPackages).build()))
.build();
}
当我从另一个类中调用createMyPayload()
来使用构建器对象时,我收到一个编译错误,它建议我的ArrayList字段数据类型不正确:
@When("I send a POST request to the endpoint (.*)$")
public void create(String endpoint, String myId, String myFirstName, String myLastName, ArrayList<String> myparts, ArrayList<String> myPackages) {
String id = "studentOne";
String myFirstName = "joe";
String myLastName = "bloggs";
String myParts = "https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l";
String myPackages = "https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1";
demoClass.post(createPayload.createMyPayload(myId, myFirstName, myLastName, myParts, myPackages), endpoint);
}
要正确传递myParts
和myPackages
给Lombok构建器对象,并在其他地方重用它们,你可以按上述方式调整createMyPayload()
方法的参数,并在其中使用DemoPackage.builder().myparts(myparts).myPackages(myPackages).build()
来创建DemoPackage对象。在DemoRequest的demoPackages字段中,你需要将DemoPackage对象放入ArrayList中。
希望这可以帮助你正确传递ArrayList字段。
英文:
I have this API request payload containing some nested fields:
{
"myId": "studentOne",
"myFirstName": "joe",
"myLastName": "bloggs",
"demoPackages":
[{
"myparts": "https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l",
"myPackages": [
"https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1"
]
}
]
}
I have this corresponding request model DTO:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoRequest {
private String myId;
private String myFirstName;
private String myLastName;
private ArrayList<DemoPackage> demoPackages;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoPackage{
private String myparts;
private ArrayList myPackages;
}
}
Now, the challenge. When creating a builder object that holds the API request, I am lost as to how to pass the ArrayList fields. I tried this:
public Object createMyPayload(String myId, String myFirstName, String myLastName, ArrayList myparts, ArrayList myPackages) { //not too sure if I am passing myParts and myPackages correctly here
return DemoRequest.builder()
.myId(myId)
.myFirstName(myFirstName)
.myLastName(myLastName)
.releasePackages(myparts)
.releasePackages(myPackages)
.build();
When I call the createMyPayload()
from another class to use the builder object, I am getting a compilation error which suggests that my ArrayList fields data type is wrong:
@When("I send a POST request to the endpoint (.*)$")
public void create(String endpoint, String myId, String myFirstName, String myLastName, ArrayList myparts, ArrayList myPackages) {
String id = "studentOne"
String myFirstName = "joe"
String myLastName = "bloggs"
String myParts = "https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l";
String myPackages = "https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1";
demoClass.post(createPayload.createMyPayload(myId, myFirstName, myLastName, myParts, myPackages), endpoint); // myParts and myPackages throw compilation error that data should be Arraylist but when I change to ArrayList, it's asking me to change back to String
How do I correctly pass myParts
and myPackages
to the lombok builder object and reuse them elsewhere?
答案1
得分: 1
这应该有效。
请注意,我已经使用了ArrayList和Array,但你提到API使用ArrayList,所以在声明中使用了List而不是ArrayList(因为这是更好的做法)。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PackageBuilderDemo {
public static void main(String[] args) {
PackageBuilderDemo packageBuilderDemo = new PackageBuilderDemo();
packageBuilderDemo.createMyPayload("studentOne", "joe", "bloggs", "https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l", Arrays.asList("https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1"));
}
public DemoRequest createMyPayload(String myId, String myFirstName, String myLastName, String myParts, List<String> myPackages) {
DemoPackage demoPackage = DemoPackage.builder().myparts(myParts).myPackages(myPackages).build();
List<DemoPackage> demoPackages = new ArrayList<>();
demoPackages.add(demoPackage);
return DemoRequest.builder()
.myId(myId)
.myFirstName(myFirstName)
.myLastName(myLastName)
.demoPackages(demoPackages)
.build();
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoRequest {
private String myId;
private String myFirstName;
private String myLastName;
private List<DemoPackage> demoPackages;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoPackage{
private String myparts;
private List<String> myPackages;
}
希望这对你有所帮助。
英文:
This should work.
Note that I have used ArrayList & Array where [] would be better, but you mentioned API using ArrayList. Used List in declarations rather than ArrayList (because that's better practise)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PackageBuilderDemo {
public static void main(String[] args) {
PackageBuilderDemo packageBuilderDemo = new PackageBuilderDemo();
packageBuilderDemo.createMyPayload("studentOne", "joe", "bloggs", "https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l", Arrays.asList(new String[] {"https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1"}));
}
public DemoRequest createMyPayload(String myId, String myFirstName, String myLastName, String myParts, List<String> myPackages) {
DemoPackage demoPackage = DemoPackage.builder().myparts(myParts).myPackages(myPackages).build();
List<DemoPackage> demoPackages = new ArrayList<>();
demoPackages.add(demoPackage);
return DemoRequest.builder()
.myId(myId)
.myFirstName(myFirstName)
.myLastName(myLastName)
.demoPackages(demoPackages)
.build();
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoRequest {
private String myId;
private String myFirstName;
private String myLastName;
private List<DemoPackage> demoPackages;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoPackage{
private String myparts;
private List<String> myPackages;
}
答案2
得分: 0
将数组直接转换为列表即可完成。
return DemoRequest.builder()
.myId(myId)
.myFirstName(myFirstName)
.myLastName(myLastName)
.myPackages(Arrays.asList(demoPackages(DemoPackage.builder()
.myparts(myParts)
.myPackages(myPackages)
.build())))
.build();
英文:
could be done by directly converting array to list.
return DemoRequest.builder()
.myId(myId)
.myFirstName(myFirstName)
.myLastName(myLastName)
.Arrays.asList(demoPackages(DemoPackage.builder()
.myparts(myParts)
.myPackages(myPackages)
.build()))
.build();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论