2.建一个java project,并把相关的jar文件添加到classpath路径下.
cxf-2.3.4.jar jetty-continuation-7.3.1.v20110307.jar jetty-http-7.3.1.v20110307.jar jetty-io-7.3.1.v20110307.jar jetty-server-7.3.1.v20110307.jar jetty-util-7.3.1.v20110307.jar servlet-api.jar wsdl4j-1.6.2.jar XmlSchema-1.4.7.jar
3.创建一个接口
package com.cxf.service; public interface HelloWorldCxfService { String sayHello(String username); }
4.创建该接口的实现类
package com.cxf.service; public class HelloWorldCxfServiceImpl implements HelloWorldCxfService { public String sayHello(String username) { return "Hello,"+username; } }
5.发布webservice
package com.cxf.server; import org.apache.cxf.frontend.ServerFactoryBean; import com.cxf.service.HelloWorldCxfService; import com.cxf.service.HelloWorldCxfServiceImpl; public class Server { public static void main(String[] args) { HelloWorldCxfServiceImpl cxfServiceImpl= new HelloWorldCxfServiceImpl(); ServerFactoryBean factoryBean=new ServerFactoryBean(); factoryBean.setAddress("http://localhost:8080/hello"); factoryBean.setServiceClass(HelloWorldCxfService.class); factoryBean.setServiceBean(cxfServiceImpl); factoryBean.create(); } }
运行Server,注意不要关闭,在控制台会打印如下信息:
2011-5-11 14:25:30 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
2011-5-11 14:25:30 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/hello
2011-05-11 14:25:30.750:INFO::jetty-7.3.1.v20110307
2011-05-11 14:25:30.859:INFO::Started SelectChannelConnector@localhost:8080
2011-05-11 14:25:30.875:INFO::started o.e.j.s.h.ContextHandler{,null}
6.客户端调用
package com.cxf.server; import org.apache.cxf.frontend.ClientProxyFactoryBean; import com.cxf.service.HelloWorldCxfService; public class Client { public static void main(String[] args) { ClientProxyFactoryBean factoryBean=new ClientProxyFactoryBean(); factoryBean.setAddress("http://localhost:8080/hello"); factoryBean.setServiceClass(HelloWorldCxfService.class); HelloWorldCxfService worldCxfService=(HelloWorldCxfService) factoryBean.create(); System.out.println(worldCxfService.sayHello("张三")); } }
运行Client代码,控制台打印如下信息:
2011-5-11 14:25:35 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
Hello,张三
到此,我们的webservice,已经成功调用了。大家是不是迫不及待的想看下wsdl文件是啥样的呢?
在浏览器中输入http://localhost:8080/hello?wsdl,即可看到wsdl文件了。其中http://localhost:8080/hello部分为代码里指定的Address。
wsdl文件信息:
<wsdl:definitions name="HelloWorldCxfService" targetNamespace="http://service.cxf.com/"> − <wsdl:types> − <xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://service.cxf.com/"> <xsd:element name="sayHello" type="tns:sayHello"/> − <xsd:complexType name="sayHello"> − <xsd:sequence> <xsd:element minOccurs="0" name="arg0" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/> − <xsd:complexType name="sayHelloResponse"> − <xsd:sequence> <xsd:element minOccurs="0" name="return" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> − <wsdl:message name="sayHelloResponse"> <wsdl:part element="tns:sayHelloResponse" name="parameters"> </wsdl:part> </wsdl:message> − <wsdl:message name="sayHello"> <wsdl:part element="tns:sayHello" name="parameters"> </wsdl:part> </wsdl:message> − <wsdl:portType name="HelloWorldCxfServicePortType"> − <wsdl:operation name="sayHello"> <wsdl:input message="tns:sayHello" name="sayHello"> </wsdl:input> <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> − <wsdl:binding name="HelloWorldCxfServiceSoapBinding" type="tns:HelloWorldCxfServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> − <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document"/> − <wsdl:input name="sayHello"> <soap:body use="literal"/> </wsdl:input> − <wsdl:output name="sayHelloResponse"> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> − <wsdl:service name="HelloWorldCxfService"> − <wsdl:port binding="tns:HelloWorldCxfServiceSoapBinding" name="HelloWorldCxfServicePort"> <soap:address location="http://localhost:8080/hello"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
最后,附上源码