繁体   English   中英

如何退货清单 <T> 在WebService(java)上

[英]How return List<T> on WebService (java)

我需要在WebService上返回不同的List,目前我在封装文件中具有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 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
}

我希望你能帮帮我

您只需要在ResponseMessage声明之后添加<T>即可告诉Java您要使用泛型。 希望这可以帮助。 请注意,它希望每个响应仅返回一种类型。 如果您需要发送不止一种类型,它可能是一个好主意,使用MapTypesLists ,而不是lstData由@ v1shnu提及。

public class ResponseMessage<T> implements Serializable {

    private final List<T> lstData;
    private final Integer code;
    private final String message;

    public ResponseMessage(Integer code, String message, List<T> lstData) {
        this.code = code;
        this.message = message;
        this.lstData = lstData;
    }
}

您可以考虑对数据访问对象执行相同的操作。

您可以创建一个HashMap ,然后再将其发送到ResponseMessage如下所示:

Map<String,List<T>> listsMap = new HashMap<>();
listsMap.put("users",userDao.readAllUsers());
listsMap.put("customers",customerDao.readAllCustomers());
listsMap.put("suppliers",supplierDao.readAllSuppliers());

ResponseMessage rm = new ResponseMessage(123, "message", listsMap);

ResponseMessage类中,可以添加如下所示的构造函数:

public ResponseMessage (Integer code, String message,Map listsMap){
    this.code = code;
    this.message = message;
    this.listUsers = (List<Users>) listsMap.get("users");
    this.listCustomers = (List<Customers>) listsMap.get("customers");
    this.listSuppliers = (List<Suppliers>) listsMap.get("suppliers");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM