i need return different Lists on a WebService, currently i have more than 15 different types in an encapsulate file but is very hard manipulate many constructors:
public class ResponseMessage implements java.io.Serializable {
private Integer code;
private String message;
private List<Users> listUsers;
private List<Products> listProducts;
private List<Customers> listCustomers;
private List<Suppliers> listSuppliers;
private List<Reports> listReports;
...
private Users userById;
private Products productById;
private Customers customerById;
private Suppliers supplierById;
...
public ResponseMessage() {
}
//My idea is something like that, not work
public ResponseMessage(Integer code, String message, List<T> lstData) {
this.code = code;
this.message = message;
this.lstData = lstData;
}
public ResponseMessage(Integer code, String message, T uniqueData) {
this.code = code;
this.message = message;
this.uniqueData = uniqueData;
}
//Currently the constructor are this, work
public ResponseMessage(Integer code, String message, List<Users> listUsers) {
this.code = code;
this.message = message;
this.listUsers = listUsers;
}
public ResponseMessage(Integer code, String message, List<Users> listUsers, List<Customers> listCustomers) {
this.code = code;
this.message = message;
this.listUsers = listUsers;
this.listCustomers = listCustomers;
}
public ResponseMessage(Integer code, String message, List<Users> listUsers, List<Customers> listCustomers, List<Suppliers> listSuppliers) {
this.code = code;
this.message = message;
this.listUsers = listUsers;
this.listCustomers = listCustomers;
this.listSuppliers = listSuppliers;
}
...
//Same history with unique result, work
public ResponseMessage(Integer code, String message, Users userById) {
this.code = code;
this.message = message;
this.userById = userById;
}
public ResponseMessage(Integer code, String message, Users userById, Products productById) {
this.code = code;
this.message = message;
this.userById = userById;
this.productById = productById;
}
//Getters and Setters
}
When i like return the constructor on the WebService i have to do it this, for example (work):
public ResponseMessage readAllSuppliers() {
List<Suppliers> lstsuppliers = new ArrayList<Suppliers>();
lstsuppliers = supplierDAO.getAllSuppliers();
//ResponseMessage(code, message, user, customer, supplier list or unique supplier)
ResponseMessage rm = new ResponseMessage(123, "reading suppliers", null, null, lstsuppliers);
return rm;
}
But i think you can do it like this for anyone list:
public ResponseMessage readAllSuppliers() {
List<Suppliers> lstsuppliers = new ArrayList<Suppliers>();
lstsuppliers = supplierDAO.getAllSuppliers();
//ResponseMessage(code, message, list or object data)
ResponseMessage rm = new ResponseMessage(123, "reading suppliers", lstsuppliers);
return rm;
}
At the end, get the info data something like this on a WebService Client:
public void getSuppliers() {
WebServiceResponse wsr = new WebServiceResponse();
ResponseMessage rm = wsr.readAllSuppliers();
System.out.println("CODE: " + rm.getCode()); //CODE: 123
System.out.println("MESSAGE: " + rm.getMessage()); //MESSAGE: reading suppliers
for (Suppliers data : rm.getLstData()) {
System.out.println("SUPPLIER INFO: " + data.getFullName());
}
//SUPPLIER INFO: Name1 Surname1
//SUPPLIER INFO: Name2 Surname2
//SUPPLIER INFO: Name3 Surname3
}
I hope you can help me