如何在 WebService (java) 上返回 List<T>
Posted
技术标签:
【中文标题】如何在 WebService (java) 上返回 List<T>【英文标题】:How return List<T> on WebService (java) 【发布时间】: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
希望你能帮到我
【问题讨论】:
查看构建器模式:en.wikipedia.org/wiki/Builder_pattern 另一种方法是发送HashMap
和keys
,即types of list (user,customers)
和corresponding lists
作为values
。您可以只有一个以HashMap
作为参数的构造函数。在您的构造函数中,您可以通过从地图获取代码来设置所有列表。由于HashMap
操作为O(1)
,因此对性能也没有太大影响。
【参考方案1】:
你只需要在你的ResponseMessage
声明之后添加<T>
来告诉Java 你想使用泛型。希望这可以帮助。请注意,它希望每个响应仅返回一种类型。如果您需要发送多个类型,最好使用Map
或Types
到Lists
,而不是@v1shnu 提到的lstData
。
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;
您可以考虑为您的数据访问对象做同样的事情。
【讨论】:
我尝试过这种方式但不起作用,当我调用供应商方法时,会显示下一个错误:javax.xml.bind.JAXBException: class Supplier 或其任何超类已知这个上下文 得到下一个错误对我来说听起来像是进步!看看这个:***.com/questions/14057932/… 但是编码是 REST 并且我使用的是 SOAP,语法发生了变化,我无法适应它:(【参考方案2】:您可以创建一个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");
【讨论】:
出现下一个错误:java.util.List is an interface, and JAXB can't handle interfaces. this problem is related to the following location: at java.util.List at public java.util.Map ResponseMessage.getListsMap()
以上是关于如何在 WebService (java) 上返回 List<T>的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Servlet 中调用 java Rest WebService
c#调用Java发布的webservice返回值为False