Home > Software engineering >  use an endpoint to dumb data using one post request
use an endpoint to dumb data using one post request

Time:09-30

Someone mentioned using JsonArray as a way to dumb this data one time using one request. But the whole concept is new to me. Can someone explain how or show me different approach to insert bunch of data using one request?

The problem is:

I need to find a way to insert the data for many customers using one post request. So instead of send multiple post request I want to include all the information in the body and insert it one time using only one post request.

  @PersonId
  private String personId;

  @NotNull(message = "invoiceDate can't be null")
  // @DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd"))
  @PastOrPresent
  @JsonFormat(pattern = "yyyy-MM-dd")
  private LocalDate invoiceDate;

  @NotNull(message = "invoiceNumber can't be null")
  private String invoiceNumber;

  @NotNull(message = "phone number can't be null")
  private Long phoneNumber;

  @NotNull(message = "invoiceAmount can't be null")
  private Double invoiceAmount; //how much does the client ows the org

  @NotNull(message = "invoiceNumber can't be null")
  private String accountNumber;

  private int msdin; //Type of subscription```

CodePudding user response:

What you can do is include the parameter List<YourObject> yourObjectList on the method that you have created to receive that request. To send multiple "YourObject" inside that request as JSON you can try as I show you:

[
    {
        // here is an instance of YourObject
        "prop1": "value1",
        "prop2": "value2"
    },
    {
        // here is another instance of YourObject
        "prop1": "value3",
        "prop2": "value4"
    }
]

CodePudding user response:

I found the answer to this problem.

So I had to make the object passed to the method of type List. Then I had to figure out a way to loop through this list, so I used for loop, ex:

for (InvoiceDTO invoices : invoiceDTO) {
//your code
}'''

Now, problem is solved.

  • Related