不兼容的数组类型可能的错误
问题描述:
所以我有这个Web服务,在一个GlassFish服务器运行并连接到包含其中包括一些webMethods的,MySQL数据库:不兼容的数组类型可能的错误
@WebMethod(operationName = "getDrops")
public Dropslog[] getDrops(@WebParam(name = "User") Users user, @WebParam(name = "MonsterID") int monsterID){
return dbmg.getDrops(user, monsterID);
}
正如你可以看到这个方法返回通过从另一个类调用此方法类型Dropslog []的变量:
public Dropslog[] getDrops(Users user, int monsterID){
Dropslog drop;
Criteria criteria = session.createCriteria(Dropslog.class);
criteria.add(Restrictions.eq("monsterId", monsterID));
drop = (Dropslog) criteria.uniqueResult();
List<Dropslog> drops = (List<Dropslog>) criteria.list();
Dropslog[] dropsArray = new Dropslog[drops.size()];
dropsArray = drops.toArray(dropsArray);
return dropsArray;
}
该方法以前返回drops
是List<Dropslog>
类型,但我改变了它,因为我读here该SOAP webservic es不能返回列表。
现在在客户端应用程序调用使用此代码将WebMethod getDrops:
public static Dropslog[] getDrops(webservice.Users user, int monsterID){
webservice.PvmWs service = new webservice.PvmWs();
webservice.ClientHandler port = service.getClientHandlerPort();
return port.getDrops(user, monsterID);
}
所以从你所看到的这个应该很好地工作,但是它并没有,而是我得到一个不兼容的类型错误小费标在去年方法的返回行的NetBeans:
incompatible types
required: Dropslog[]
found: List<Dropslog>
令人惊讶,当我改变它,因为NetBeans表明它编译和作品。我想知道为什么会发生这种情况,如果它的某种错误或NetBeans以某种方式使用旧文件来编译代码,或者如果Java做某种自动生成?
答
JAXB中数组的默认输出类型是一个列表。
从我所知道的,
List<T>
和Array
在JAXB中没有任何区别。在内部,即使你声明的WS操作是这样的:
public Shape[] echoShapes(Shape[] input)
JAXB将创建一个
List<Shape>
实例来保持解组的结果,然后用List.toArray()
把它转换成数组类型。
有趣...谢谢! – 2013-03-20 01:04:31