0
votes

I want to get multiple JSON data which I am passing into Postman in Body as Json format.

If I am passing single json data like : "{"firstName":"riya","lastName":"soni"}" so I can get it in my Java API mentioned as below :

@Path("patient")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public String getPatient(Patient patient) {
System.out.println(patient.getFirstName());
System.out.println(patient.getLastName());
return "ok";
}

It is working properly. But if I want to pass data like [{"firstName":"riya","lastName":"soni"},{"firstName":"shreya","lastName":"patel"}]

and getting in API like below :

@Path("patientDetail")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public String getPatientDetails(@RequestBody List<Patient> patient) {
for (Patient p : patient) {
System.out.println(p.getFirstName());
System.out.println(p.getLastName());
}
return "ok";
}

When I am hitting request from postman by passing multiple JSON data as I have mentioned, even getPateintDetails method is not calling, how to pass and get multiple json data in Rest API. Please give me the solution or reference link for the same.

2
Please add error codes and exceptions you are getting - Mahesh Kumar

2 Answers

0
votes

Check this small working example which are looking for..

Track.java

public class Track {
    String title;
    String singer;
    //getters and setters
    @Override
    public String toString() {
        return "Track [title=" + title + ", singer=" + singer + "]";
    }
}

JSONService.java

@Path("/json/check")
public class JSONService {
    @POST
    @Path("/postManyAsString")
    @Consumes(MediaType.APPLICATION_JSON)
    public String createManyTrackInJSON(String jsonStr) throws Exception {
        JSONObject inputObject = new JSONObject(jsonStr);
        if(inputObject.get("tracks") instanceof JSONObject)
        {
            JSONObject jsonObject = (JSONObject) inputObject.get("tracks");
            System.out.println(jsonObject.get("title"));
            System.out.println(jsonObject.get("singer"));
        }
        else if(inputObject.get("tracks") instanceof JSONArray)
        {
            JSONArray jsonArray = (JSONArray) inputObject.get("tracks");
            for(int i=0; i<jsonArray.length(); i++)
            {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                System.out.println(jsonObject.get("title"));
                System.out.println(jsonObject.get("singer"));
            }
        }
        else
        {
            System.out.println(inputObject.get("tracks"));
        }
        return "Okay";
    }
}

URL: http://localhost:8080/RESTfulExample/rest/json/check/postMany input data:

[{
    "title" : "hai1",
    "singer" : "sree1"
},
{
    "title" : "hai2",
    "singer" : "sree2"
},
{
    "title" : "hai3",
    "singer" : "sree3"
}]

enter image description here

-1
votes

Try the JSON by passing in the below format.

{
  "data" : [
    {"firstName":"riya","lastName":"soni"}, 
    {"firstName":"shreya","lastName":"patel"}
 ]
}

Parse the JSON and get the data variable first and use it to get each object in the array.