2012-12-06 105 views
2

嗨我想从web服务返回一个列表。我的代码是在axis2 web服务中返回列表

public class WebListTest { 
    public List serviceFunction(String arg1,String arg2) 
    { 
    List list=new ArrayList(); 
     list.add(arg1); 
     list.add(arg2); 
      return list;  

     } 
} 

但在WSDL创作,我发现

<xs:element minOccurs="0" name="return" nillable="true" type="xs:anyType"/> 

而当从客户端调用此WebService我得到异常

org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Any type element type has not been given 
    at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430) 
    at webservice1.WebListTestStub.fromOM(WebListTestStub.java:1622) 
    at webservice1.WebListTestStub.serviceFunction(WebListTestStub.java:191) 
    at webservice1.ServiceTest.main(ServiceTest.java:24) 
Caused by: java.lang.Exception: org.apache.axis2.databinding.ADBException: Any type element type has not been given 
    at webservice1.WebListTestStub$ServiceFunctionResponse$Factory.parse(WebListTestStub.java:917) 
    at webservice1.WebListTestStub.fromOM(WebListTestStub.java:1616) 
    ... 2 more 
Caused by: org.apache.axis2.databinding.ADBException: Any type element type has not been given 
    at org.apache.axis2.databinding.utils.ConverterUtil.getAnyTypeObject(ConverterUtil.java:1612) 
    at webservice1.WebListTestStub$ServiceFunctionResponse$Factory.parse(WebListTestStub.java:895) 
    ... 3 more 

现在我do.please帮助。

回答

6

从Axis2 POJO Web服务对象返回数据时,您不应该使用Java集合类型,而应该返回对象或基本类型的数组。 WSDL不允许Java集合数据结构。请记住,Web服务需要可以从任何语言访问,并且不会像Java一样使用相同的集合框架。

所以,做这样的事情:

public class WebListTest { 
    public String[] serviceFunction(String arg1,String arg2) { 

    List<String> stringList=new ArrayList<String>(); 

    stringList.add(arg1); 
    stringList.add(arg2); 

    return stringList.toArray(new String[stringList.size()]); 
    } 
} 
+0

它无法投射到数组。其显示错误。花花公子。 – Krishna

+0

不知道为什么,为我工作。也许更改不同的版本? – Wrench

+0

不要因为Arraylist无法投射到String数组中。 bcoz其实你不能使用集合类,因为它不支持SOAP服务。我们必须使用数组或字符串数​​据类型 – Krishna

0

您可以从“列表”更改返回类型“列表<字符串>”,它会正常工作。

public class WebListTest { 
    public List<String> serviceFunction(String arg1,String arg2) 
    { 
     List<String> list=new ArrayList(); 
     list.add(arg1); 
     list.add(arg2); 
     return list;  

    } 
}