CXF学习(1) 使用CXF实现简单的HelloWorld
项目要用到cxf,对WebService这方面的东西不熟悉,甚至都没有用过!从JavaEye上看到一位大哥把自己录制的视频,甚是高兴,学习一下,补充一下自己!
视频分为7部分,我挨着顺序给写到播客上,希望能够帮助那些想学习cxf又无从下手的朋友!
1. 使用cxf实现简单的HelloWorld
1)使用myeclipse新建一个Java项目,项目名HelloWorld。
2)把下载的cxf-2.3.1的lib下的所有jar文件添加进来,有可能有些文件用不到。
3)建立WebService接口,Package:test,Name:HelloWorld
package test;public interface HelloWorld {public String sayHello(String name);}
package test;public class HelloWorldImpl implements HelloWorld {@Overridepublic String sayHello(String name) { //为了表明这个方法确实被调用了,写一个输出语句System.out.println("Say Hello is called");return "Hello " + name;}}
package test;import javax.jws.WebService;@WebServicepublic interface HelloWorld {public String sayHello(String name);}
package test;import javax.jws.WebService;@WebServicepublic class HelloWorldImpl implements HelloWorld {@Overridepublic String sayHello(String name) {//为了表明这个方法确实被调用了,写一个输出语句System.out.println("Say Hello is called");return "Hello " + name;}}
(1)新建一个工厂(2)为工厂设置WebService具体实现类,注意是具体实现类(3)为WebService设置地址(4)只要通过这样的设置,就能够通过Jetty把我们的WebService发布到这个地址(5)得到Server(6)启动Server
package test;import org.apache.cxf.endpoint.Server;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;public class MainServer {public static void main(String[] args) {JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();factory.setServiceClass(HelloWorldImpl.class);factory.setAddress("http://localhost:8080/HelloWorld");Server server = factory.create();server.start();}}
该 XML 文件并未包含任何关联的样式信息。文档树显示如下。 ?<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/">?<wsdl:types>?<xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0"><xs:element name="sayHello" type="tns:sayHello"/><xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>?<xs:complexType name="sayHello">?<xs:sequence><xs:element minOccurs="0" name="name" type="xs:string"/></xs:sequence></xs:complexType>?<xs:complexType name="sayHelloResponse">?<xs:sequence><xs:element minOccurs="0" name="sayHelloResult" type="xs:string"/></xs:sequence></xs:complexType></xs: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="HelloWorld">?<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="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld"><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="HelloWorldImplService">?<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort"><soap:address location="http://localhost:8080/HelloWorld"/></wsdl:port></wsdl:service></wsdl:definitions>
(1)新建一个Jax代理工厂(2)为WebService设置地址(3)设置接口的类型,因为不知道具体类是什么(4)通过create方法得到HelloWorld(5)调用HelloWorld方法
package test;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class HelloWorldClient {public static void main(String[] args) {JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setAddress("http://localhost:8080/HelloWorld");factory.setServiceClass(HelloWorld.class);HelloWorld helloWorld = (HelloWorld) factory.create();System.out.println(helloWorld.sayHello("KingAragorn"));}}
Hello KingAragorn