微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Webservice实例-使用CXF实现

 

 

 

参考:https://www6.software.ibm.com/developerworks/cn/education/java/j-cxf/index.html

 

 

 

wsdl的描述:

      1.   <?xml version="1.0" encoding="UTF-8" ?> 
      2. <wsdl:deFinitions name="ISurveyServiceService" targetNamespace="http://cxf.ws/" xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://cxf.ws/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      3. <wsdl:types>
      4. <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://cxf.ws/" xmlns:tns="http://cxf.ws/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      5.   <xs:element name="Vote" type="tns:Vote" /> 
      6.   <xs:element name="VoteResponse" type="tns:VoteResponse" /> 
      7. <xs:complexType name="Vote">
      8. <xs:sequence>
      9.   <xs:element minOccurs="0" name="arg0" type="xs:string" /> 
      10.   <xs:element name="arg1" type="xs:int" /> 
      11.   </xs:sequence>
      12.   </xs:complexType>
      13. <xs:complexType name="VoteResponse">
      14. <xs:sequence>
      15.   <xs:element minOccurs="0" name="return" type="xs:string" /> 
      16.   </xs:sequence>
      17.   </xs:complexType>
      18.   </xs:schema>
      19.   </wsdl:types>
      20. <wsdl:message name="Vote">
      21.   <wsdl:part element="tns:Vote" name="parameters" /> 
      22.   </wsdl:message>
      23. <wsdl:message name="VoteResponse">
      24.   <wsdl:part element="tns:VoteResponse" name="parameters" /> 
      25.   </wsdl:message>
      26. <wsdl:portType name="ISurveyService">
      27. <wsdl:operation name="Vote">
      28.   <wsdl:input message="tns:Vote" name="Vote" /> 
      29.   <wsdl:output message="tns:VoteResponse" name="VoteResponse" /> 
      30.   </wsdl:operation>
      31.   </wsdl:portType>
      32. <wsdl:binding name="ISurveyServiceServiceSoapBinding" type="tns:ISurveyService">
      33.   <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
      34. <wsdl:operation name="Vote">
      35.   <soap:operation soapAction="" style="document" /> 
      36. <wsdl:input name="Vote">
      37.   <soap:body use="literal" /> 
      38.   </wsdl:input>
      39. <wsdl:output name="VoteResponse">
      40.   <soap:body use="literal" /> 
      41.   </wsdl:output>
      42.   </wsdl:operation>
      43.   </wsdl:binding>
      44. <wsdl:service name="ISurveyServiceService">
      45. <wsdl:port binding="tns:ISurveyServiceServiceSoapBinding" name="ISurveyServicePort">
      46.   <soap:address location="http://localhost:8080/CXF_Spring_Survey//SurveyWebService" /> 
      47.   </wsdl:port>
      48.   </wsdl:service>
      49.   </wsdl:deFinitions>

      客户端测试:

    1.  

      import org.springframework.context.ApplicationContext;
    2. import org.springframework.context.support.ClasspathXmlApplicationContext;
    3. import ws.cxf.ISurveyService;
    4. public class Test {
    5.     public static void main(String[] args) {
    6.         System.out.println("test");
    7.         // 加载客户端的配置定义
    8.         ApplicationContext context = new ClasspathXmlApplicationContext("beanRefClient.xml");
    9.         // 获取定义的Web Service Bean
    10.         ISurveyService surveyService = (ISurveyService)context.getBean("surveyServiceClient");
    11.         // 1、定义调查投票的变量与内容,用来z发送给服务
    12.         String username = "Test";
    13.         int point = 88;
    14.         // 调用方法进行服务消费
    15.         String result = surveyService.Vote(username,point);
    16.         System.out.println("Result:" + result);
    17.         // 2、传递不一样的调查投票内容
    18.         username = "Michael";
    19.         point = 100;
    20.         // 再次调用方法进行服务消费,得到不一样的结果
    21.         result = surveyService.Vote(username,point);
    22.         System.out.println("Result:" + result);
    23.         // 3、第三次传递与调用
    24.         username = "Jordan";
    25.         point = 9;
    26.         result = surveyService.Vote(username,point);
    27.         System.out.println("Result:" + result);
    28.     }
    29. }
    test
  1. 2008-11-16 23:04:26 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@1484a05: display name [org.springframework.context.support.ClasspathXmlApplicationContext@1484a05]; startup date [Sun Nov 16 23:04:26 CST 2008]; root of context hierarchy
  3. 2008-11-16 23:04:26 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions
  4. 信息: Loading XML bean deFinitions from class path resource [beanRefClient.xml]
  5. 2008-11-16 23:04:26 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions
  6. 信息: Loading XML bean deFinitions from class path resource [meta-inf/cxf/cxf.xml]
  7. 2008-11-16 23:04:26 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions
  8. 信息: Loading XML bean deFinitions from class path resource [meta-inf/cxf/cxf-extension-soap.xml]
  9. 2008-11-16 23:04:27 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions
  10. 信息: Loading XML bean deFinitions from class path resource [meta-inf/cxf/cxf-servlet.xml]
  11. 2008-11-16 23:04:27 org.springframework.context.support.AbstractApplicationContext obtainFreshbeanfactory
  12. 信息: Bean factory for application context [org.springframework.context.support.ClasspathXmlApplicationContext@1484a05]: org.springframework.beans.factory.support.DefaultListablebeanfactory@fb6c5f
  13. 2008-11-16 23:04:27 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
  14. 信息: Bean 'org.apache.cxf.bus.spring.Jsr250BeanPostProcessor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  15. 2008-11-16 23:04:27 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
  16. 信息: Bean 'org.apache.cxf.bus.spring.BusExtensionPostProcessor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
  17. 2008-11-16 23:04:27 org.springframework.beans.factory.support.DefaultListablebeanfactory preInstantiateSingletons
  18. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListablebeanfactory@fb6c5f: defining beans [cxf,org.apache.cxf.bus.spring.BusWiringbeanfactoryPostProcessor,org.apache.cxf.bus.spring.Jsr250BeanPostProcessor,org.apache.cxf.bus.spring.BusExtensionPostProcessor,org.apache.cxf.resource.ResourceManager,org.apache.cxf.configuration.Configurer,org.apache.cxf.binding.BindingFactoryManager,org.apache.cxf.transport.DestinationFactoryManager,org.apache.cxf.transport.ConduitinitiatorManager,org.apache.cxf.wsdl.WSDLManager,org.apache.cxf.phase.PhaseManager,org.apache.cxf.workqueue.WorkQueueManager,org.apache.cxf.buslifecycle.BusLifeCycleManager,org.apache.cxf.endpoint.ServerRegistry,org.apache.cxf.endpoint.ServerLifeCycleManager,org.apache.cxf.endpoint.ClientLifeCycleManager,org.apache.cxf.transports.http.QueryHandlerRegistry,org.apache.cxf.endpoint.EndpointResolverRegistry,org.apache.cxf.headers.HeaderManager,org.apache.cxf.catalog.oasisCatalogManager,org.apache.cxf.endpoint.ServiceContractResolverRegistry,org.apache.cxf.binding.soap.soapBindingFactory,org.apache.cxf.binding.soap.soapTransportFactory,org.apache.cxf.binding.soap.customEditorConfigurer,org.apache.cxf.transport.servlet.ServletTransportFactory,surveyServiceClient.proxyFactory,surveyServiceClient]; root of factory hierarchy
  19. Result:谢谢你的投票!你的投票分数通过审核!
  20. Result:你不能重复进行投票!
  21. Result:谢谢你的投票!你的投票分数太低!

 

开发过程中如果出现如下错误

页面报错:
java.lang.NoSuchMethodError: org.w3c.dom.Document.setXmlStandalone(Z)V
org.apache.cxf.transport.http.WSDLQueryHandler.updateDoc(WSDLQueryHandler.java:253)
org.apache.cxf.transport.http.WSDLQueryHandler.writeResponse(WSDLQueryHandler.java:201)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:152)
org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:174)
org.apache.cxf.transport.servlet.AbstractCXFServlet.doGet(AbstractCXFServlet.java:156)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

 

考虑换掉tomcat版本即可。

 

IBM提供的下载地址:https://www6.software.ibm.com/developerworks/cn/education/java/j-cxf/CXF_Spring_Survey_Src.war

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐