加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservice实例-使用CXF实现

发布时间:2020-12-17 02:37:24 所属栏目:安全 来源:网络整理
导读:? ? ? 参考:https://www6.software.ibm.com/developerworks/cn/education/java/j-cxf/index.html ? ? ? wsdl的描述: ?? ? xml ? version = "1.0" ? encoding = "UTF-8" ? ? ? -? wsdl:definitions ? name = "ISurveyServiceService" ? targetNamespace = "h

?

?

?

参考: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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读