2014-04-15 53 views
0

我在我的Eclipse項目中有一個代理類,用於項目使用Web服務。代理類看起來像這樣(從嚮導自動生成的代碼)在Eclipse中更改Web服務java代理方法返回類型

public class WebServiceUpg2SoapProxy implements org.uppgift2.www.WebServiceUpg2Soap { 
private String _endpoint = null; 
private org.uppgift2.www.WebServiceUpg2Soap webServiceUpg2Soap = null; 

public WebServiceUpg2SoapProxy() { 
    _initWebServiceUpg2SoapProxy(); 
} 

public WebServiceUpg2SoapProxy(String endpoint) { 
    _endpoint = endpoint; 
    _initWebServiceUpg2SoapProxy(); 
} 

private void _initWebServiceUpg2SoapProxy() { 
try { 
    webServiceUpg2Soap = (new org.uppgift2.www.WebServiceUpg2Locator()).getWebServiceUpg2Soap(); 
    if (webServiceUpg2Soap != null) { 
    if (_endpoint != null) 
    ((javax.xml.rpc.Stub)webServiceUpg2Soap)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint); 
    else 
    _endpoint = (String)((javax.xml.rpc.Stub)webServiceUpg2Soap)._getProperty("javax.xml.rpc.service.endpoint.address"); 
} 

} 
catch (javax.xml.rpc.ServiceException serviceException) {} 
} 

public String getEndpoint() { 
    return _endpoint; 
} 

public void setEndpoint(String endpoint) { 
    _endpoint = endpoint; 
    if (webServiceUpg2Soap != null) 
    ((javax.xml.rpc.Stub)webServiceUpg2Soap)._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint); 

} 

public org.uppgift2.www.WebServiceUpg2Soap getWebServiceUpg2Soap() { 
    if (webServiceUpg2Soap == null) 
    _initWebServiceUpg2SoapProxy(); 
    return webServiceUpg2Soap; 
} 

public org.uppgift2.www.ObjectOwner[] getObjectOwner() throws java.rmi.RemoteException{ 
    if (webServiceUpg2Soap == null) 
    _initWebServiceUpg2SoapProxy(); 
    return webServiceUpg2Soap.getObjectOwner(); 
} 

public org.uppgift2.www.RealEstateBroker[] getRealEstateBroker() throws java.rmi.RemoteException{ 
    if (webServiceUpg2Soap == null) 
    _initWebServiceUpg2SoapProxy(); 
    return webServiceUpg2Soap.getRealEstateBroker(); 
} 

public org.uppgift2.www.Showing[] getShowing() throws java.rmi.RemoteException{ 
    if (webServiceUpg2Soap == null) 
    _initWebServiceUpg2SoapProxy(); 
    return webServiceUpg2Soap.getShowing(); 
} 

public org.uppgift2.www.ProspectiveBuyer[] getProspectiveBuyers() throws java.rmi.RemoteException{ 
    if (webServiceUpg2Soap == null) 
    _initWebServiceUpg2SoapProxy(); 
    return webServiceUpg2Soap.getProspectiveBuyers(); 
} 

public org.uppgift2.www.RealEstateObject[] getRealEstateObjects() throws java.rmi.RemoteException{ 
    if (webServiceUpg2Soap == null) 
    _initWebServiceUpg2SoapProxy(); 
    return webServiceUpg2Soap.getRealEstateObjects(); 
    } 
} 

我在另一個類的方法創建代理類的實例,並調用它的方法

public TableModel getObjectOwner() { 
WebServiceUpg2SoapProxy proxy2 = new WebServiceUpg2SoapProxy(); 

    List<ObjectOwner> oo = null; 
    try { 
     oo = proxy2.getObjectOwner(); 
    } catch (RemoteException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return buildTableModel(oo);  
} 

這使我一個錯誤說

類型不匹配:不能從ObjectOwner []轉換到列表

我的問題是;如何將代理的返回類型從ObjectOwner []更改爲List?這可以在嚮導中完成嗎?

我知道在Visual Studio中,您只需右鍵單擊解決方案資源管理器中的服務器引用,然後轉到配置參考,然後在提示窗口中對其進行更改。這可以在Eclipse中完成嗎?

在此先感謝。

回答

1

錯誤非常明確:您不能將ObjectOwner[](數組)分配給List<ObjectOwner>(實現List接口的類的實例)。取而代之的是,你可以改變你的代碼來適應這個數組並將其轉換成使用Arrays#asList列表:

oo = Arrays.asList(proxy2.getObjectOwner()); 

注意,通過這樣做,你不能在這個列表中添加新的元素。如果要修改列表的狀態,倒不如創建新ArrayList(或List界面的另一個希望的實現),並在數組中添加元素:

//Since Java 7 
oo = new ArrayList<>(Arrays.asList(proxy2.getObjectOwner())); 

//For Java 5 and 6 
oo = new ArrayList<ObjectOwner>(Arrays.asList(proxy2.getObjectOwner())); 
+0

是的你是對的。這將解決我的問題。謝謝。 – Marcus

0

我猜,你正在尋找這個:

列表大小將用於形成新的ObjectOwner,後來它使用toArray方法轉換爲所需的結果。

ObjectOwner[] ow= list.toArray(new ObjectOwner[list.size()]);