【问题标题】:Incompatible array type possible bug on webservice methodwebservice方法上不兼容的数组类型可能存在错误
【发布时间】:2013-03-29 03:02:59
【问题描述】:

所以我有这个在 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&lt;Dropslog&gt; 但我在阅读 here 时更改了它,因为 SOAP Web 服务无法返回列表。

现在客户端的应用程序使用以下代码调用 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 是否进行了某种自动转换?

【问题讨论】:

    标签: java web-services jakarta-ee netbeans


    【解决方案1】:

    JAXB 中数组的默认输出类型是列表。

    据我所知,List&lt;T&gt;Array 在 JAXB 中没有任何区别。在内部,即使您这样声明您的 WS 操作:

    public Shape[] echoShapes(Shape[] input)
    

    JAXB 将创建一个 List&lt;Shape&gt; 实例来保存 Unmarshal 结果,然后使用 List.toArray() 将其转换为数组类型。

    OTN Discussion Forums : Webservices ...Array or List ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      相关资源
      最近更新 更多