阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Apache CXF 框架应用实战

113次阅读
没有评论

共计 3159 个字符,预计需要花费 8 分钟才能阅读完成。

一、概述

Apache CXF 提供了用于方便地构建和开发 WebService 的可靠基础架构。它允许创建高性能和可扩展的服务,可以部署在 Tomcat 和基于 Spring 的轻量级容器中,也可以部署在更高级的服务器上,例如 Jboss、WebSphere 或 WebLogic。CXF 提供了以下功能:

  • WebService 服务标准支持:

Java API for XML Web Services (JAX-WS)
SOAP
WebService 描述语言(Web Services Description Language,WSDL)
消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
WS-Basic Profile
WS-Addressing
WS-Policy
WS-ReliableMessaging
WS-Security

  • 前端建模 :CXF 允许使用不同的前端 API 来创建 Service。如 CXF 允许使用简单的工厂 Bean 并通过 JAX-WS 实现来创建 WebService,允许创建动态 WebService 客户端。
  • 工具支持 :CXF 提供了在 Java Bean、WebService 和 WSDL 之间进行转换的工具,提供了对 Maven 和 Ant 集成的支持,并无缝地支持 Spring 集成。
  • RESTful 支持 :CXF 支持 Restful,并支持 Java 平台的 JAX-RS 实现。
  • 对不同传输和绑定的支持 :CXF 支持不同数据类型的传输,除了支持 SOAP 和 HTTP 协议绑定外,还支持 JAXB 和 AEGIS 绑定。
  • 对非 XML 绑定的支持 :CXF 支持非 XML 绑定,如 JSON、CORBA、JBI 和 SCA 等。
  • Code First 和 Xml First:CXF 支持使用 Code First 或者 Xml First 的方式创建 WebService。

二、使用 CXF 内置 jetty 发布 WebService

maven:

<properties>  
        <cxf.version>3.1.4</cxf.version>  
</properties>

<dependency>  
        <groupId>org.apache.cxf</groupId>  
        <artifactId>cxf-rt-frontend-jaxws</artifactId>  
        <version>${cxf.version}</version>  
</dependency>  
  
<dependency>  
        <groupId>org.apache.cxf</groupId>  
        <artifactId>cxf-rt-transports-http</artifactId>  
        <version>${cxf.version}</version>  
</dependency>  
          
          
<!-- 使用 cxf 内置的 jetty 服务器发布 WebService -->  
<dependency>  
        <groupId>org.apache.cxf</groupId>  
        <artifactId>cxf-rt-transports-http-jetty</artifactId>  
        <version>${cxf.version}</version>  
</dependency>  

接口:

@WebService
public interface HelloService{public String helloCxf();

    public String hello(String name);public User getUser(int id);

    public void saveUser(User user);

} 

实现:

@WebService(serviceName = "helloService",
        endpointInterface = "cn.lg.ws.hellocxf.HelloService"
)
public class HelloServiceImpl implements HelloService{

    @Override
    public String helloCxf(){return "Hello CXF!";
    }

    @Override
    public String hello(String name)
    {return "Hello" + name;
    }

    @Override
    public User getUser(int id) {User u1 = new User();
        return u1;
    }

    @Override
    public void saveUser(User user) {System.out.println(user.toString());
    }

} 

发布:

public class PublishService{/**
     * 使用 CXF 的 JaxWsServerFactoryBean 发布服务
     * @param
     */
    public static void main(String[] args) {JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        factory.setServiceClass(HelloService.class);
        // 服务发布地址 
        factory.setAddress("http://localhost:8088/soap/hello");
        factory.setServiceBean(new HelloServiceImpl());
        factory.create();
        System.out.println("publish success");
    }
} 

使用浏览器访问 http://localhost:8088/soap/hello?wsdl 可以看到 wsdl 如下,则说明发布成功

Apache CXF 框架应用实战

 

使用 CXF 在客户端调用 WebService:

public class ClientTest{public static void main(String[] args) {JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
        jwpfb.setServiceClass(HelloService.class);
        jwpfb.setAddress("http://localhost:8088/sop/hello");
        HelloService hs = (HelloService) jwpfb.create();
        System.out.println(hs.getUser(101));
        Q.p(hs.hello("luangeng"));
    }
}

相关类可通过以下命令产生:

wsimport -p com.ickes.cxf.client -keep http://localhost:8088/sop/hello?wsdl

使用 Spring 和 Tomcat 发布 CXF WebService  见 http://www.linuxidc.com/Linux/2017-03/141982.htm

Apache CXF 快速入门基础教程 http://www.linuxidc.com/Linux/2014-05/101383.htm

Apache CXF 实战 http://www.linuxidc.com/Linux/2012-05/59887.htm

Apache CXF 快速入门基础教程  http://www.linuxidc.com/Linux/2014-05/101383.htm

Apache CXF 拦截器 Interceptor 实现 WebServices 用户验证  http://www.linuxidc.com/Linux/2016-12/138291.htm

Apache CXF 的详细介绍 :请点这里
Apache CXF 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-03/141981.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3159字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中