WolfSoft

常用链接

统计

最新评论

CXF HelloWorld 篇

Apache CXF简介

  Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

Apache CXF功能特性

  CXF 包含了大量的功能特性,但是主要集中在以下几个方面:   支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL 优先开发,也支持从 Java 的代码优先开发模式。容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非 XML 的类型绑定,比如:JSON 和 CORBA。

依赖jar:


接口定义:          
@WebService
public interface HelloWorld extends Remote  {
    
public @WebResult(name="sayHelloResult")String sayHi(@WebParam(name="text") String text);
}


实现类:
@WebService(targetNamespace="com.wolfsoft.demo" )
public class HelloWorldImpl implements HelloWorld {
    
    
public @WebResult(name="sayHelloResult") String sayHi(@WebParam(name="text")String text) {
        
return "Hello " + text;
    }

    
}


服务启动:
public class StartService {
    @Test
    
public void publishingService(){
        
        System.out.println(
"------------------Starting Server--------------------");
        
        
/*
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "
http://localhost:9000/helloWorld";
        Endpoint.publish(address, implementor);
*/

        
        
/*HelloWorldImpl implementor = new HelloWorldImpl();
        JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
        svrFactory.setServiceClass(HelloWorld.class);
        svrFactory.setAddress("
http://localhost:9000/helloWorld");
        svrFactory.setServiceBean(implementor);
        svrFactory.getInInterceptors().add(new LoggingInInterceptor());
        svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
        svrFactory.create();
*/

        
        JaxWsServerFactoryBean factory 
= new JaxWsServerFactoryBean();
        factory.setServiceClass(HelloWorldImpl.
class);
        factory.setAddress(
"http://localhost:9000/helloWorld");         
        Server server 
= factory.create();
        server.start();
    }

}


客户端调用:
public class AccessingSerice {
    
    @Test
    
public void  getSerice(){
        
final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.getInInterceptors().add(
new LoggingInInterceptor());
        factory.getOutInterceptors().add(
new LoggingOutInterceptor());
        factory.setServiceClass(HelloWorld.
class);
        factory.setAddress(
"http://localhost:9000/helloWorld");
        
final HelloWorld client = (HelloWorld) factory.create();

        
final String reply = client.sayHi("World");
        System.out.println(
"Server said: " + reply);
        System.exit(
0); 
   }

}


客户端调用结果:
Server said: Hello World
 

posted on 2010-11-04 16:37 SuperWolf 阅读(656) 评论(0)  编辑  收藏 所属分类: CXF


只有注册用户登录后才能发表评论。


网站导航: