0

Currently I'm doing an API to fire JSON array of array to mysql. But I don't know how to receive the data to the model class when fire the API. I tried to use ArrayList[] but still cannot make it.

Here is the data that I need to fire.

enter image description here

enter image description here

Controller :

//insert Trip
    @RequestMapping(value = "inserting", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String add(@RequestBody ArrayList<Trips[]>[] c) {
        LOGGER.debug("start insert to database...");
        for(int i=0;i<c.length;i++){
             //trip = c[i];
            tripsService.saveTrips(trip); 
        }   
        LOGGER.debug("insert successfull...");
        return c.toString();
    }
11
  • can you switch the json structure you have? Commented Mar 29, 2021 at 1:17
  • cannot is fix only can json array of array Commented Mar 29, 2021 at 1:18
  • try doing in your controller ArrayList<Trips[]>[] to ArrayList<ArrayList<Object>> Commented Mar 29, 2021 at 1:20
  • in order you can map to this Trips Object you need to have a json structure: key -> value, you have no it, so it will not be able to map Commented Mar 29, 2021 at 1:22
  • ArrayList<ArrayList<Object>> able to work but how to convert it to Trips model class? bcsz i need to .save(Trips). Commented Mar 29, 2021 at 1:26

2 Answers 2

1

It is not recommended to pass Array in JSON format. You should put one wrapper class over you entity trips. something like:

class MyTrips {
    List<Trips> trips = new ArrayList<>();
    //Getter and Setter 
}

At the end receive something like:

//insert Trip
    @RequestMapping(value = "inserting", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String add(@RequestBody MyTrips myTrips) {
        ArrayList<Trips[]>[] c = myTrips.getTrips();
        LOGGER.debug("start insert to database...");
        for(int i=0;i<c.length;i++){
             //trip = c[i];
            tripsService.saveTrips(trip); 
        }   
        LOGGER.debug("insert successfull...");
        return c.toString();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

but I get this error "cannot convert from List<Trips> to ArrayList<Trips[]>[]" at myTrips.getTrips()
0

I think it is not possible to map directly to the object you want, for the I suggest you switch the incoming data type, something like:

public @ResponseBody String add(@RequestBody ArrayList<ArrayList<Object>> c) {

Then, in the controller doing a iteration over each element you have in the array, instantiate the Trips Object, doing use of setter methods, set the values of Trips object

for(int i=0;i<c.length;i++){
    List<Object> tripIncoming = c.get(i);
    Trips trip = new Trips();
    trip.setPropertie1(tripIncoming.get(0))
    trip.setPropertie2(tripIncoming.get(1))

    ....
    .... rest properties
    tripsService.saveTrips(trip); 
} 

4 Comments

but I have 150++ field to set for the Trips class, any other dynamic way to do it?
do you mean the Trip class has 150 fields?
Actually, I got another model class with 150 ++ field for another project. Bcsz of asking question on here, so I just use the Trip class with less field to try it out with the same method
I think you can create a custom method, where you can set the different properties, perhaps you have to write much code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.