网站建设知识
Apache CXF学习笔记一
2025-07-22 10:00  点击:0

环境:

[1]JDK 1.7.x

[2]Eclipse Mars.2 J2EE Version

[3]Spring 3.1.2.RELEASE

[4]CXF 3.0.9

概述:

测试Spring+CXF实现Webservice服务端和客户端的技术可行性。

正文

服务端

Step1:新建maven工程后,配置pom.xml

  4.0.0  com.nuoke  WSServerTest  war  0.0.1-SNAPSHOT  WCServerTestMavenWebapp  maven.apache.org        3.1.2.RELEASE    3.0.9             org.springframeworkspring-context${spring.version}org.springframeworkspring-web${spring.version}      org.apache.cxf      cxf-rt-frontend-jaxws      ${cxf.version}                   org.apache.cxf      cxf-rt-transports-http      ${cxf.version}                commons-io    commons-io    2.4        WCServerTest                                     org.apache.maven.plugins                  maven-compiler-plugin                                        1.7                      1.7                      UTF-8                                                                org.apache.maven.plugins                  maven-surefire-plugin                                                              true                                              

Step2:web.xml

    WSServerTest            cription>Apache CXF Endpointcription>        cxf        cxf        org.apache.cxf.transport.servlet.CXFServlet        1                cxf        /servicesprivate static final QName SERVICE_NAME= new QName("kagula/", "IService");private static final QName PORT_NAME = new QName("kagula/", "IServicePort");public static void main(String[] args) throws Exception{Service service = Service.create(SERVICE_NAME);String endpointAddress = "localhost:8080/WSServerTest/services/hello";// Add a port to the Serviceservice.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);           IService is = service.getPort(IService.class);    System.out.println(is.sayHi("inuyasha"));        //    MyPub mypub = new MyPub("Hello,Everybody!",1,2,new Date());    MyPackage mypackage = new MyPackage("c2s",mypub);    is.sayHi2(mypackage);}//end function}//end class

备注

[1]在“mvnrepository/artifact”下可以查询依赖库是否存在。

[2]CXF3.0.9的示例代码可以通过下面的地址下载

apache.org/dyn/closer.lua/cxf/3.0.9/apache-cxf-3.0.9.zip