如何在Apache Axis2 Web Service中返回自定义对象

时间:2012-09-05 21:05:46

标签: java web-services apache wsdl axis2

我一直在尝试使用Eclipse Juno,Maven和java2wsdl插件部署Apache Axis2 WebService。然而,我一直遇到一个问题,我认为这是由于映射,希望有人可以指出我正确的方向。

我正在尝试在我的WebService上调用一个接受参数并返回自定义对象的操作,但到目前为止没有运气。

我的代码

为了说话,我有以下课程:

package com.example.models;
public class MyClass {
    // Does something
}

我在以下操作中返回MyClass列表:

package com.example.service;
import com.example.models;
public class MyService {
    public List<MyClass> getExampleData(// a parameter) {  
        // Do something
        return aListOfMyClass;
    }
}

我的services.xml文件:

<serviceGroup>
  <service name="MyWebService" targetNamespace="http://com.example.service.MyWebService/">
    <description>JNLPGenerator service</description>
    <schema schemaNamespace="http://com.example.service.MyWebService/" />
    <parameter name="ServiceClass" locked="false">com.example.service.MyWebService</parameter>
    <operation name="getExampleData">
      <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
  </service>
</serviceGroup>

错误:

当我部署WebService时,我收到以下错误消息,这是由于未映射类MyClass。

<Exception>org.apache.axis2.AxisFault: org.apache.axis2.AxisFault: Mapping qname not fond for the package: com.example.models
    at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
    at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:161)
    at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
    at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:173)
    at org.apache.axis2.transport.http.util.RESTUtil.invokeAxisEngine(RESTUtil.java:135)
    at org.apache.axis2.transport.http.util.RESTUtil.processURLRequest(RESTUtil.java:130)
    at org.apache.axis2.transport.http.AxisServlet$RestRequestProcessor.processURLRequest(AxisServlet.java:817)
    at org.apache.axis2.transport.http.AxisServlet.doGet(AxisServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: org.apache.axis2.AxisFault: Mapping qname not fond for the package: com.example.models
    at org.apache.axis2.databinding.utils.BeanUtil.getPropertyQnameList(BeanUtil.java:261)
    at org.apache.axis2.databinding.utils.BeanUtil.getPullParser(BeanUtil.java:67)
    at org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl.processProperties(ADBXMLStreamReaderImpl.java:994)
    at org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl.next(ADBXMLStreamReaderImpl.java:831)
    at org.apache.axis2.util.StreamWrapper.next(StreamWrapper.java:71)
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:672)
    at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214)
    at org.apache.axiom.om.impl.llom.OMSerializableImpl.build(OMSerializableImpl.java:78)
    at org.apache.axiom.om.impl.llom.OMElementImpl.build(OMElementImpl.java:745)
    at org.apache.axiom.om.impl.llom.OMElementImpl.detach(OMElementImpl.java:723)
    at org.apache.axiom.om.impl.llom.OMNodeImpl.setParent(OMNodeImpl.java:105)
    at org.apache.axiom.om.impl.llom.OMElementImpl.addChild(OMElementImpl.java:297)
    at org.apache.axiom.om.impl.llom.OMElementImpl.addChild(OMElementImpl.java:213)
    at org.apache.axiom.soap.impl.llom.SOAPBodyImpl.addChild(SOAPBodyImpl.java:231)
    at org.apache.axis2.rpc.receivers.RPCUtil.processResponseAsDocLitWrapped(RPCUtil.java:422)
    at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:138)
    ... 25 more
Caused by: org.apache.axis2.AxisFault: Mapping qname not fond for the package: com.example.models
    at org.apache.axis2.databinding.utils.BeanUtil.getPropertyQnameList(BeanUtil.java:117)
    ... 40 more
</Exception>

所以我想知道的是如何做到这一点。我读过一个similar post,但说实话,我仍然不明白我必须添加这个映射来解决问题。还要指出部署Web服务很好,wsdl也可以生成。

很抱歉很长的帖子,但希望有人能帮帮我..

1 个答案:

答案 0 :(得分:2)

问题是由于Java2WSDL无法使用Java Collections,因此解决方案是在使用Web Services并使用Arrays时不使用Java Collections。

基本上为了让我的网络服务正常运行,我做了以下更改。

package com.example.service;
import com.example.models;
public class MyService {
    public MyClass[] getExampleData(// a parameter) {  
        // Do something
        return myClassArray;
    }
}

对于想要了解有关Java Collections的Java2WSDL inability的更多信息的任何人,您可以参考Java web services: Axis2 Data Binding by IBM DeveloperWorks