[英]Pass multiple JSON data in Request Body of Postman and Get into Java Rest API using Jersy(JXRS)
I want to get multiple JSON data which I am passing into Postman in Body as Json format.我想获取多个 JSON 数据,这些数据以 Json 格式传递给 Body 中的 Postman。
If I am passing single json data like : "{"firstName":"riya","lastName":"soni"}" so I can get it in my Java API mentioned as below :如果我传递单个 json 数据,例如:"{"firstName":"riya","lastName":"soni"}" 所以我可以在我的 Java API 中得到它,如下所述:
@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"}]
但是如果我想传递像 [{"firstName":"riya","lastName":"soni"},{"firstName":"shreya","lastName":"patel"}] 这样的数据
and getting in API like below :并进入如下 API:
@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.当我通过传递多个 JSON 数据来满足邮递员的请求时,即使 getPateintDetails 方法也没有调用,如何在 Rest API 中传递和获取多个 json 数据。 Please give me the solution or reference link for the same.
请给我相同的解决方案或参考链接。
Check this small working example which are looking for..检查这个正在寻找的小型工作示例。
Track.java轨道.java
public class Track {
String title;
String singer;
//getters and setters
@Override
public String toString() {
return "Track [title=" + title + ", singer=" + singer + "]";
}
}
JSONService.java 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: URL: http://localhost:8080/RESTfulExample/rest/json/check/postMany输入数据:
[{
"title" : "hai1",
"singer" : "sree1"
},
{
"title" : "hai2",
"singer" : "sree2"
},
{
"title" : "hai3",
"singer" : "sree3"
}]
Try the JSON by passing in the below format.通过传入以下格式来尝试 JSON。
{
"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.解析 JSON 并首先获取数据变量,并使用它来获取数组中的每个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.