有没有办法将API ArrayList字段传递给POJO构建器对象?

huangapple go评论67阅读模式
英文:

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);
}

要正确传递myPartsmyPackages给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:

 {
      &quot;myId&quot;: &quot;studentOne&quot;,
      &quot;myFirstName&quot;: &quot;joe&quot;,
      &quot;myLastName&quot;: &quot;bloggs&quot;,
      &quot;demoPackages&quot;: 
       [{
          &quot;myparts&quot;: &quot;https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l&quot;,
          &quot;myPackages&quot;: [
           &quot;https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1&quot;
            ]
        }
       ]
    }

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&lt;DemoPackage&gt; 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(&quot;I send a POST request to the endpoint (.*)$&quot;)
public void create(String endpoint, String myId, String myFirstName, String myLastName, ArrayList myparts, ArrayList myPackages) {


String id = &quot;studentOne&quot;
String myFirstName = &quot;joe&quot;
String myLastName = &quot;bloggs&quot;
String myParts = &quot;https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l&quot;;
String myPackages = &quot;https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1&quot;;


    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&#39;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(&quot;studentOne&quot;, &quot;joe&quot;, &quot;bloggs&quot;, &quot;https://example.com/myparts/a1234567-5d25-9gf1-23ua-45pb3874265l&quot;, Arrays.asList(new String[] {&quot;https:/example.com/myPackages/0sk98926-939a-444a-95ta-8eb40125f7r1&quot;}));
	}
	
	public DemoRequest createMyPayload(String myId, String myFirstName, String myLastName, String myParts, List&lt;String&gt; myPackages) { 
	    DemoPackage demoPackage = DemoPackage.builder().myparts(myParts).myPackages(myPackages).build();
		List&lt;DemoPackage&gt; demoPackages = new ArrayList&lt;&gt;();
				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&lt;DemoPackage&gt; demoPackages;
  
}


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DemoPackage{
    private String myparts;
    private List&lt;String&gt; 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();

huangapple
  • 本文由 发表于 2023年2月7日 01:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定