【发布时间】:2017-11-01 01:17:08
【问题描述】:
我需要在 WebService 上返回不同的列表,目前我在一个封装文件中有超过 15 种不同的类型,但很难操作许多构造函数:
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
}
当我喜欢在 WebService 上返回构造函数时,我必须这样做,例如(工作):
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;
}
但我认为你可以对任何人列表这样做:
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;
}
最后,在 WebService 客户端上获取类似这样的信息数据:
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
}
希望你能帮到我
【问题讨论】:
-
另一种方法是发送
HashMap和keys,即types of list (user,customers)和corresponding lists作为values。您可以只有一个以HashMap作为参数的构造函数。在您的构造函数中,您可以通过从地图获取代码来设置所有列表。由于HashMap操作为O(1),因此对性能也没有太大影响。
标签: java list generics methods return