ITeye博客

WebService CXF学习(入门篇2):HelloWorld

iaspecwang · 2013-08-09 09:22 · WEBSERVICE · 企业架构 · Markdown

理论联系实际,单单只讲理论那就成了纸上谈兵,用一个HelloWorld Demo可来说明事更加直观。那下面咱们就开始进行讲解: 
   首先到apache官方网下载apache-cxf-2.2.2,地址:http://cxf.apache.org/ 
   新建一个Java Project,导入cxf常用.jar包 

Java代码  收藏代码

  1. commons-logging-1.1.1.jar
  2. geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
  3. geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
  4. geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
  5. geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
  6. geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
  7. geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
  8. geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
  9. jaxb-api-2.1.jar
  10. jaxb-impl-2.1.12.jar
  11. jetty-6.1.21.jar
  12. jetty-util-6.1.21.jar
  13. neethi-2.0.4.jar
  14. saaj-api-1.3.jar
  15. saaj-impl-1.3.2.jar
  16. wsdl4j-1.6.2.jar
  17. wstx-asl-3.2.8.jar
  18. XmlSchema-1.4.5.jar
  19. xml-resolver-1.2.jar
  20. cxf-2.2.2.jar

   接下就是HelloWorld Demo开发了 
   第一步:新建一个webservice接口 

Java代码  收藏代码

  1. @WebService
  2. public interface IHelloWorld {
  3. //@WebParam给参数命名,提高可代码可读性。此项可选
  4. blic String sayHi(@WebParam(name="text") String text);
  5. }

   通过注解@WebService申明为webservice接口 
   第二步,实现WebService接口 

Java代码  收藏代码

  1. @WebService
  2. public class HelloWorldImpl implements IHelloWorld {
  3. public String sayHi(String name) {
  4. System.out.println("sayHello is called by " + name);
  5. return "Hello " + name;
  6. }
  7. }

   第三步,创建服务端 

Java代码  收藏代码

  1. public class Server {
  2. private Server(){
  3. IHelloWorld helloWorld = new HelloWorldImpl();
  4. //创建WebService服务工厂
  5. JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
  6. //注册WebService接口
  7. factory.setServiceClass(IHelloWorld.class);
  8. //发布接口
  9. factory.setAddress("http://localhost:9000/HelloWorld");
  10. factory.setServiceBean(helloWorld);
  11. //创建WebService
  12. factory.create();
  13. };
  14. public static void main(String[] args) throws InterruptedException{
  15. //启动服务端
  16. new Server();
  17. System.out.println("Server ready...");
  18. //休眠一分钟,便于测试
  19. Thread.sleep(1000*60);
  20. System.out.println("Server exit...");
  21. System.exit(0);
  22. }
  23. }

    第四步,创建客户端 
  

Java代码  收藏代码

  1. public class Client {
  2. private Client(){};
  3. public static void main(String[] args){
  4. //创建WebService客户端代理工厂
  5. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  6. //注册WebService接口
  7. factory.setServiceClass(HelloWorld.class);
  8. //设置WebService地址
  9. factory.setAddress("http://localhost:9000/HelloWorld");
  10. IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
  11. System.out.println("invoke webservice...");
  12. System.out.println("message context is:"+iHelloWorld.sayHi("
  13. Josen"));
  14. System.exit(0);
  15. }
  16. }

    最后是万事俱备,只欠测试了 
    首先,运行服务端程序 
    其次,打开浏览器,在地址栏中输入http://localhost:9000/HelloWorld?wsdl(因为cxf自带了一个jetty服务器),查看接口是否发布成功,如里浏览器页面显示下面内容,证明接口发布成功 

Java代码  收藏代码

  1. <wsdl:definitions name="IHelloWorldService" targetNamespace="http://client.itdcl.com/">
  2. <wsdl:types>
  3. <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://client.itdcl.com/">
  4. <xsd:element name="sayHi" type="tns:sayHi"/>
  5. <xsd:complexType name="sayHi">
  6. <xsd:sequence>
  7. <xsd:element minOccurs="0" name="text" type="xsd:string"/>
  8. </xsd:sequence>
  9. </xsd:complexType>
  10. <xsd:element name="sayHiResponse" type="tns:sayHiResponse"/>
  11. <xsd:complexType name="sayHiResponse">
  12. <xsd:sequence>
  13. <xsd:element minOccurs="0" name="return" type="xsd:string"/>
  14. </xsd:sequence>
  15. </xsd:complexType>
  16. </xsd:schema>
  17. </wsdl:types>
  18. <wsdl:message name="sayHi">
  19. <wsdl:part element="tns:sayHi" name="parameters">
  20. </wsdl:part>
  21. </wsdl:message>
  22. <wsdl:message name="sayHiResponse">
  23. <wsdl:part element="tns:sayHiResponse" name="parameters">
  24. </wsdl:part>
  25. </wsdl:message>
  26. <wsdl:portType name="IHelloWorld">
  27. <wsdl:operation name="sayHi">
  28. <wsdl:input message="tns:sayHi" name="sayHi">
  29. </wsdl:input>
  30. <wsdl:output message="tns:sayHiResponse" name="sayHiResponse">
  31. </wsdl:output>
  32. </wsdl:operation>
  33. </wsdl:portType>
  34. <wsdl:binding name="IHelloWorldServiceSoapBinding" type="tns:IHelloWorld">
  35. <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
  36. <wsdl:operation name="sayHi">
  37. <soap:operation soapAction="" style="document"/>
  38. <wsdl:input name="sayHi">
  39. <soap:body use="literal"/>
  40. </wsdl:input>
  41. <wsdl:output name="sayHiResponse">
  42. <soap:body use="literal"/>
  43. </wsdl:output>
  44. </wsdl:operation>
  45. </wsdl:binding>
  46. <wsdl:service name="IHelloWorldService">
  47. <wsdl:port binding="tns:IHelloWorldServiceSoapBinding" name="IHelloWorldPort">
  48. <soap:address location="http://localhost:9000/HelloWorld"/>
  49. </wsdl:port>
  50. </wsdl:service>
  51. </wsdl:definitions>

    最后,运行客户端程序,看看效果如果。 
    这一节就讲到此为止,下节对WSDL定义进行讲解,便于对上面这个Demo进行很好的消化,同时对后面章节启个辅塾作用。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。