你好,游客 登录 注册 搜索
背景:
阅读新闻

使用CXF开发WebService简单实例

[日期:2011-12-13] 来源:Linux社区  作者:joy_joe [字体:大 中 小]

第一次使用CXF开发WebService,一个简单小例子。

相关教程: CXF使用Tomcat发布WebService的简单例子 http://www.linuxidc.com/Linux/2011-12/49309.htm

Server:

HelloWorld.java

  1. package joe.test.cxf;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface HelloWorld {  
  8. String sayHi(@WebParam(name="text") String text);  
  9. }  

HelloWorldImpl.java

  1. package joe.test.cxf;  
  2.   
  3. public class HelloWorldImpl implements HelloWorld{  
  4.     public String sayHi(String name){  
  5.         return "Hello " + name;   
  6.     }  
  7. }  

DeployHelloWorldService.java

  1. package joe.test.cxf;  
  2.   
  3. import javax.jws.WebService;  
  4. import javax.xml.ws.Endpoint;  
  5.   
  6.   
  7. @WebService(endpointInterface = "joe.test.cxf.HelloWorld",  
  8.             serviceName = "HelloWorld")  
  9. public class DeployHelloWorldService {  
  10.       
  11.     public static void deployService(){  
  12.         System.out.println("Start Service...");  
  13.         HelloWorldImpl helloWorld = new HelloWorldImpl();  
  14.         String address = "http://localhost:9000/helloworld";  
  15.         Endpoint.publish(address, helloWorld);  
  16.     }  
  17.       
  18.     public static void main(String[] args) throws InterruptedException{  
  19.         deployService();  
  20.         System.out.println("Start Server");  
  21.         Thread.sleep(1000 * 60);  
  22.         System.out.println("End Server");  
  23.         System.exit(0);  
  24.     }  
  25. }  

Client:

testClient.java

  1. package joe.test.cxf;  
  2.   
  3. import org.apache.cxf.interceptor.LoggingInInterceptor;  
  4. import org.apache.cxf.interceptor.LoggingOutInterceptor;  
  5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  6.   
  7. public class testClient {  
  8.     public static void main(String[] args){  
  9.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  10.         factory.getInInterceptors().add(new LoggingInInterceptor());  
  11.         factory.getOutInterceptors().add(new LoggingOutInterceptor());  
  12.         factory.setServiceClass(HelloWorld.class);  
  13.         factory.setAddress("http://localhost:9000/helloworld");  
  14.         HelloWorld client = (HelloWorld) factory.create();  
  15.   
  16.         String reply = client.sayHi("Joe");  
  17.         System.out.println("Server said: " + reply);  
  18.         System.exit(0);   
  19.     }  
  20. }  

HelloWorld.java

  1. package joe.test.cxf;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface HelloWorld {  
  8.     String sayHi(@WebParam(name="text") String text);  
  9. }  
linux
相关资讯       CXF 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款