您的位置:首页 > 移动开发 > Objective-C

java axis调用cxf 的坑object is not an instance of declaring class while invoking public

2016-11-09 15:30 686 查看
简单说一下出错的场景,项目需要为别人系统开放webservice,于是在springmvc框架下集成cxf开发webservice接口,网上教程一大堆,小有波折,参考的
https://my.oschina.net/long0419/blog/192788,我用的maven工程,cxf依赖如下:
<dependency>  

        <groupId>org.apache.cxf</groupId>  

        <artifactId>cxf-rt-frontend-jaxws</artifactId>  

        <version>${cxf.version}</version>  

 </dependency>  

<dependency>  

        <groupId>org.apache.cxf</groupId>  

        <artifactId>cxf-rt-frontend-jaxws</artifactId>  

        <version>${cxf.version}</version>  

 </dependency>  

 <dependency>  
       <groupId>org.apache.cxf</groupId>  
       <artifactId>cxf-rt-transports-http</artifactId>  
       <version>${cxf.version}</version>  

</dependency>  
       <!-- Jetty is needed if you're are not using the CXFServlet -->  

<dependency>  
       <groupId>org.apache.cxf</groupId>  
       <artifactId>cxf-rt-transports-http-jetty</artifactId>  
       <version>${cxf.version}</version>  

</dependency> 

<properties>下加入版本

<cxf.version>2.2.3</cxf.version> ,

web.xml下配置classpath:webservice/webservice.xml,配置servlet,这个按照https://my.oschina.net/long0419/blog/192788的介绍,完全没问题,

值得注意的是,在webservice.xml中

<beans
xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd         http://cxf.apache.org/jaxws         http://cxf.apache.org/schemas/jaxws.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd">    
   
<import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

    <!-- 接口的实现类声明 -->
    <jaxws:endpoint id="testservice" 

        implementorClass="com.fy.util.webservice.TestWebServiceImpl" 

        address="/testws" />

</beans>

<jaxws:endpoint id="testservice" 

        implementorClass="com.fy.util.webservice.TestWebServiceImpl" 

        address="/testws" />这里我这样定义的接口实现位置,完成接口,项目发布后,没有错误,打开浏览器,输入http://localhost:8880/fs_door/ws/testws?wsdl,页面显示如下:

到此,我以为webservice服务的开发已经ok了,但自己用写客户端测试,用的axis,jar包网上很容易找到,如需要请留言,测试代码如下:

Service service = new Service();
Call call;
String result="";

try {
call = (Call)service.createCall();
call.setTargetEndpointAddress("http://localhost:8880/fs_door/ws/testws");// 远程调用路径
call.setOperationName(new QName("http://webservice.util.fy.com/", "test"));

call.addParameter("name", // 参数名
XMLType.XSD_STRING,// 参数类型:String
ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
call.setUseSOAPAction(true);
result=(String) call.invoke(new Object[]{"asdf"});

} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();

catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(result);

测试别的webservice接口都ok,可是测试上面的接口时,一直报错:

AxisFault

 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server

 faultSubcode: 

 faultString: object is not an instance of declaring class while invoking public java.lang.String com.fy.util.webservice.TestWebServiceImpl.test(java.lang.String) with params [asdf].

 faultActor: 

 faultNode: 

 faultDetail: 
{http://xml.apache.org/axis/}stackTrace:object is not an instance of declaring class while invoking public java.lang.String com.fy.util.webservice.TestWebServiceImpl.test(java.lang.String) with params [asdf].
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.md.test.Main.main(Main.java:63)

{http://xml.apache.org/axis/}hostname:PYQZF70WEDVIKCN

object is not an instance of declaring class while invoking public java.lang.String com.fy.util.webservice.TestWebServiceImpl.test(java.lang.String) with params [asdf].
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.md.test.Main.main(Main.java:63)

反复的检查客户端调用程序无果,最后怀疑服务端有问题,才回到服务端有关于webservice的所有地方。

问题原因:<jaxws:endpoint id="testservice" 
        implementorClass="com.fy.util.webservice.TestWebServiceImpl" address="/testws"
/>

这一句换成

<bean id="hello" class="com.fy.util.webservice.TestWebServiceImpl"
/>

<jaxws:endpoint id="testws" implementor="#hello" address="/testws"
/>

问题解决,至于为什么,尚在学习,希望知道的大神能指点留言,非常感谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息