CXF step by step
在http://incubator.apache.org/cxf/download.html 下载最新版本的CXF。之后解压到D:/CXF
示例的开发分为三步
l 设置编译环境
l 写一个简单的 JAX-WS 服务
l 通过JAX-WS API把你的服务发布
l 通过客户端调用你的服务
1. 设置编译环境
打开eclipse(其他的IDE也可以),引入如下的类库(也可以省事一点,把CXF/lib路径下的所有类库都引入)
commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
jetty-6.1.5.jar
jetty-util-6.1.5.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
引入Spring相关的jars (只对):
aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar
引入CXF相关的jar:
cxf-2.0-incubator.jar
2. 编写服务
在eclipse里创建一个普通的JAVA PROJECT。
服务的例子代码在 “D:/cxf/samples/java_first_jaxws”路径下。最方便的办法是直接把代码引入项目,也可以手工创建这几个类,然后把代码拷贝过去。
首先写一个服务接口,例子中的HelloWorld.java。我们要注意的是这个例子使用了JSR181规范中的声明“@WebService”。
package demo.hw.server; import javax.jws.WebService; @WebServicepublic interface HelloWorld { String sayHi(String text);}下一步实现这个服务接口,例子中的HelloWorldImpl.java。这个例子代码中通过“@WebService”标签告诉CXF运行环境把 “HelloWorld”这个服务发布为WSDL。
package demo.hw.server; import javax.jws.WebService; @WebService(endpointInterface = "demo.hw.server.HelloWorld", serviceName = "HelloWorld")public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { return "Hello " + text; }}
3. 发布你的服务
你要编写一个服务器程序。例子中的Server.java。Server.java有两个关键的作用,一个是启动CXF的运行环境,第二个是把上面的服务器实现发布到CXF运行环境中去。
public class Server {
protected Server() throws Exception {
// START SNIPPET: publish
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld"; Endpoint.publish(address, implementor);
// END SNIPPET: publish
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
4.调用你的服务,客户端程序程序的编写
客户端例子代码为Client.java。客户端调用了Client.java的代码如下
package demo.hw.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import demo.hw.server.HelloWorld;
public final class Client {
private static final QName SERVICE_NAME
= new QName("http://server.hw.demo/", "HelloWorld");
private static final QName PORT_NAME
= new QName("http://server.hw.demo/", "HelloWorldPort");
private Client() {
}
public static void main(String args[]) throws Exception {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:9000/helloWorld";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWorld hw = service.getPort(HelloWorld.class);
System.out.println(hw.sayHi("World"));
}
}
通过上面的四步,HelloWorld的例子就编写完了。可以编译执行了,由于程序中使用了标签,所以你必须要使用5.0的编译器。在Eclipse里面,配置如下图
运行过程:
1. 首先运行Server.java。看到如下信息,说明服务器启动成功
……
信息: jetty-6.1.5
2007-10-6 7:54:40 sun.reflect.NativeMethodAccessorImpl invoke0
信息: Started SelectChannelConnector@0.0.0.0:9000
Server ready...
2. 也可通过在IE中访问http://localhost:9000/helloWorld?wsdl来查看服务器的运行情况和服务的发布情况。
3. 运行Client.java,看到调用结果。
另外:
在使用hello world的例子中,client.java遇到了" The type class is not generic; it cannot be parameterized with arguments <T> "这样的出错提示。安装了一个新版本的JAVA 5.0 update 13后问题解决。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/woohooli/archive/2007/10/06/1812681.aspx