所以我有一个在 glassfish 服务器中运行的 Web 服务,并连接到一个 mysql 数据库,该数据库包含多个 Web 方法,其中:
@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 Web 服务无法返回列表。
现在客户端的应用程序使用以下代码调用 Web 方法 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 中数组的默认输出类型是列表。
From what I know,
List<T>
andArray
doesn't make any difference in JAXB. Internally, even you declare your WS operation like this:
public Shape[] echoShapes(Shape[] input)
JAXB will create a
List<Shape>
instance to hold the Unmarshal results, then useList.toArray()
to convert it into array type.
关于java - Web 服务方法上可能存在不兼容的数组类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513181/