I want to get three different types of objects (Object list) from a single service call. It's OK to get different objects from different call. Is it OK do do it in a single call?
If the number of different objects you want to return is limited to 3, then you can create a result object, which has properties for each of the three different objects you want to return
public class Result {
private ResultOne resultOne;
private ResultTwo resultTwo;
private ResultThree resultThree;
public ResultOne getResultOne() {
return resultOne;
}
public void setResultOne(ResultOne resultOne) {
this.resultOne = resultOne;
}
public ResultTwo getResultTwo() {
return resultTwo;
}
public void setResultTwo(ResultTwo resultTwo) {
this.resultTwo = resultTwo;
}
public ResultThree getResultThree() {
return resultThree;
}
public void setResultThree(ResultThree resultThree) {
this.resultThree = resultThree;
}
}
If you want to return a true list of objects, see Does rest supports arraylist of objects?
This can be done by two ways: Way 1: Make the required POJO class for three different type of objects, you want to return and return this POJO class as output of web service call.
Way 2: Create a HashMap object.Push the key and corresponding object into the HashMap and return that HashMap object. eg
HashMap < String, List < String >> multipleObjectReturn = new HashMap < String, List < String >> ();
List < String > listObject1 = new ArrayList < String > ();
listObject1.add("List 1 Value2");
multipleObjectReturn.put("List Object 1", listObject1);
List < String > listObject2 = new ArrayList < String > ();
listObject1.add("List 2 Value1");
multipleObjectReturn.put("List Object 2", listObject2);
Finally,
return multipleObjectReturn.
Above, code is used to return different type of list objects. If you want to return any type of objects, then use Object type instead of List < String >
in HashMap.
HashMap < String, Object > multipleObjectReturn = new HashMap < String, Object > ();
HashMap < String, String > mapObject1 = new HashMap < String, String > ();
mapObject1.put("Key1", "Value1");
multipleObjectReturn.put("ListObject1", listObject1);
multipleObjectReturn.put("ListObject2", listObject2);
multipleObjectReturn.put("MapObject1", mapObject1);
return multipleObjectReturn;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.