2012-06-05 56 views
0

我有以下的Web服務:無法從Web服務使用二維物體的返回值

@WebMethod(operationName = "getCaseTypeNamesAndIDs") 
public Object [][] getCaseTypeNamesAndIDs() { 
    Object [][] nameIDs; 
    int [] ids; 

    ids = LOKmeth.getAllCaseTypes(); 
    nameIDs = new Object [ids.length][2]; 

    for(int ct = 0; ct < ids.length; ct++) 
    { 
     nameIDs[ct][0] = LOKmeth.getCaseTypeName(ids[ct]); 
    } 

    for(int ct = 0; ct < ids.length; ct++) 
    { 
     nameIDs[ct][1] = ids[ct]; 
    } 

    return nameIDs; 
} 

這是爲了填補與「箱式」的名字第一個維度中的字符串和第二的形式具有「case type」ID的維度由ints組成。

當我測試web服務它輸出:

方法返回

java.util.List : "[[email protected], [email protected], [email protected]]" 

SOAP響應

<?xml version="1.0" encoding="UTF-8"?> 
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:getCaseTypeNamesAndIDsResponse xmlns:ns2="http://LOK_WS/"> 
      <return> 
       <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Bugg</item> 
       <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">3</item> 
      </return> 
      <return> 
       <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Felrapport</item> 
       <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">1</item> 
      </return> 
      <return> 
       <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Printer on fire</item> 
       <item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:int">2</item> 
      </return> 
     </ns2:getCaseTypeNamesAndIDsResponse> 
    </S:Body> 
</S:Envelope> 

我的數字,該方法返回是陣列存儲器的引用。

SOAP響應包含正確的數據。

我的問題是這樣的: 我如何提取我的jsp頁面中的數據?

我試圖做類似如下(有一些變化):

<% 
try 
{ 
    lok_ws.CaseManagementWs_Service service = new lok_ws.CaseManagementWs_Service(); 
    lok_ws.CaseManagementWs port = service.getCaseManagementWsPort(); 

    java.util.List<net.java.dev.jaxb.array.AnyTypeArray> caseTypeNames = null; 

    caseTypeNames = port.getCaseTypeNamesAndIDs(); 

    Object[][] result = new Object[1][]; 
    result[0] = caseTypeNames.toArray(); 

    out.println("<option value=\"\">"); 
    out.println(result[0][0].toString()); 
    out.println("</option>"); 
} catch (Exception ex) 
{ 
    // TODO handle custom exceptions here 
} 
%> 

我讀了A java.lang.ClassCastException while accessing web service method written in java. jaxb並試圖按照他的解決方案,但它並沒有幫助。

我該怎麼做才能使用該方法給我的引用?

在此先感謝!

回答

0

我和你有同樣的問題,我期待2個暗淡的陣列。幸運的是我的朋友幫助了我!

這是我的解決方案:

StringArray[] user = response.get_return(); 

for(int i = 0; i < user.length; i++){ 
    String[] a = user[i].getItem(); 

    for(String c:a) 
     System.out.println(c); 
} 
1

我覺得你的web服務而應返回一個特定應用的對象,而不是一個普通的2維數組列表。 您的web服務響應包含一個包含字符串和int字段的「case type」對象列表。 因此,這將是更好的創建(在網絡服務端)與此類似的類:

class CaseType { 
    private int id; 
    private String name; 
    // getters, setters 
} 

和Web服務應該基本上返回CaseType對象(List<CaseType>)的列表。它不僅可以解決您的Web服務問題,而且還可以改進通用設計,因爲使用顯式和命名類是自行記錄並且更易於閱讀和維護的。