利用CXF框架开发WebService

接口:

在接口中添加WebService的注解,将其标注为WebService的服务接口。

@WebService  
public interface Hello {
  public String sayHello(String str);
}

实现类:

1 public class HelloImpl implements Hello {  
2     
3       public String sayHello(String str) {  
4           System.out.println("调用成功");  
5           return "Hello " + str;  
6       }  
7 }  

 

 

WebService服务器端:

 

1  public class MainServer {  
2      public static void main(String[] args) {  
3          JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();   
4          factory.setAddress("http://localhost:8080/HelloWebService");  
5          factory.setServiceClass(HelloImpl.class);  
6          Server server = factory.create();  
7          server.start();  
8      }  
9  }  

 

 

 

 

factory.setAddress("http://localhost:8080/HelloWebService");

设置服务在服务器上部署的位置

factory.setServiceClass(HelloImpl.class);

设置服务暴露的接口实现类

 

完成之后运行MainServer中的main方法。

注:因为CXF框架中有Jetty 6 服务器,所以这个的demo发布在其中运行。

 

之后打开浏览器,输入:

http://localhost:8080/HelloWebService?wsdl

正常打开有wsdl显示则成功

编写客户端:

 public class Client {  
     public static void main(String[] args) {  
         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
         factory.setServiceClass(Hello.class);  
         factory.setAddress("http://localhost:8080/HelloWebService");  
         Hello hello = (Hello)factory.create();  
           
         System.out.println(hello.sayHello("weberyb"));  
     }  
   
 }  

 

factory.setServiceClass(Hello.class);

设置访问服务器端的指定接口。

factory.setAddress("http://localhost:8080/HelloWebService");

设置访问的服务的地址。

factory.create()

创建代理对象以供远程调用

 

之后运行Client中main方法,可以在控制台的服务器端看见如下输出:

  1. ....................  
  2. 2009-8-13 14:00:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass  
  3. 信息: Creating Service {http://test/}HelloService from class test.Hello  
  4. Hello weberyb  

说明客户端调用WebService成功。

至此,这个简单的WebService开发完毕

转载于:https://www.cnblogs.com/ABFM/p/5112300.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值