2012年3月11日星期日

Apache CXF Demo


Apache CXF官方网站的A Simple JAX-WS Service依葫芦画瓢走了一遍,没有太多东东,这里仅仅是记录下来已方便自己回忆。这里我使用Eclipse作为开发工具。
根据官方网站可以把这个Sample分成四部分来完成,所以我这里也按照这四部分来记录:
1. 搭建Apache CXF环境
a) 创建一个新的Java Project
b) 添加相关的jar包,所有的jar包都可以在CXF官方下载站点的apache-cxf-2.2.8.zip下载包里找到.
CXF依赖的jar包:
commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.8.jar
XmlSchema-1.4.5.jar
xml-resolver-1.2.jar
Spring依赖的jar包:
aopalliance-1.0.jar
spring-core-2.5.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-web-2.5.5.jar
CXF的jar包:
cxf-2.2.3.jar
2. 编写Service端代码
a) Service端接口类:
[java] view plaincopy
  1. import javax.jws.WebParam;  
  2. import javax.jws.WebService;  
  3.   
  4. @WebService  
  5. public interface HelloService {  
  6.     public String sayHello(@WebParam(name="text")String text);  
  7. }  


b) Service端实现类:
[java] view plaincopy
  1. import javax.jws.WebService;  
  2.   
  3. @WebService  
  4. public class HelloServiceImpl implements HelloService {      
  5.     public String sayHello(String text) {  
  6.         return "Hello ".concat(text);  
  7.     }  
  8. }  


3. 发布Service端代码
a) 发布代码
[java] view plaincopy
  1. protected Server() throws Exception {  
  2.     // START SNIPPET: publish  
  3.     System.out.println("Starting Server");  
  4.     HelloServiceImpl implementor = new HelloServiceImpl ();  
  5.     String address = "http://localhost:9000/helloWorld";  
  6.     Endpoint.publish(address, implementor );  
  7.     // END SNIPPET: publish  
  8. }  


b) Main 函数

[c-sharp] view plaincopy
  1. public static void main(String args[]) throws Exception {  
  2.     new Server();  
  3.     System.out.println("Server ready...");  
  4.   
  5.     Thread.sleep(5 * 60 * 1000);  
  6.     System.out.println("Server exiting");  
  7.     System.exit(0);  
  8. }  


c) 运行Server端代码,并用浏览器打开“http://localhost:9000/helloWorld?wsdl”来验证WSDL是否生产。
4. 编写Client端代码
a) Client端代码:
[c-sharp] view plaincopy
  1. public static void main(String[] args) {  
  2.     JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  3.     factory.setServiceClass(HelloService.class);  
  4.     factory.setAddress("http://localhost:9000/helloWorld");  
  5.     HelloService client = (HelloService) factory.create();  
  6.           
  7.     String reply = client.sayHello("aaa");  
  8.     System.out.println("Server said :".concat(reply));  
  9.     System.exit(0);  
  10. }  

b) 先启动服务器端代码,在启动Client端代码,可看到输出为“Hello aaa”.

没有评论:

发表评论