如何在WebService上返回List <t>(java)

时间:2017-05-31 09:45:11

标签: java list generics methods return

我需要在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
}

我希望你能帮助我

2 个答案:

答案 0 :(得分:0)

您只需在<T>声明后添加ResponseMessage即可告诉Java您想要使用泛型。希望这可以帮助。请注意,它希望仅针对每个响应返回一种类型。如果您需要发送多种类型,最好使用Map Types Lists代替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; } } 。< / p>

In [46]: df = pd.read_table(io.StringIO("""    broker1  broker2  broker3  ticker
    ...: 0   val1     val2     val3     tick1
    ...: 1   val4     None     val6     tick2"""), sep='\s+')

In [47]: df = df.replace('None', float('nan'))

In [48]: melted = df.melt(id_vars=("ticker",),
    ...:                  var_name="broker",
    ...:                  value_name="ticker_b").\
    ...:     dropna().\
    ...:     sort_values(["ticker", "ticker_b"])

In [49]: melted
Out[49]: 
  ticker   broker ticker_b
0  tick1  broker1     val1
2  tick1  broker2     val2
4  tick1  broker3     val3
1  tick2  broker1     val4
5  tick2  broker3     val6

您可以考虑为数据访问对象做同样的事情。

答案 1 :(得分:0)

您可以在将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"); } 课程中,您可以添加如下构造函数:

// Position of the vertex as seen from the current camera
gl_Position = projection * modelview * vec4(VertexPosition, 1.0);