JBoss Fuse - Apache CXF



在本章中,我们将讨论什么是 Apache CXF 以及它如何在开发 SOAP 和 REST Web 服务中提供帮助。

什么是 Apache CXF?

Apache CXF 是一个 Web 服务开发框架,可用于开发 SOAP 和 REST Web 服务。CXF 完全符合JAX-RS 和 JAX-Ws标准。

它是目前使用最广泛的 Web 服务开发框架。CXF 从 Axis2 中学习并改进,Axis2 现在正逐渐被 CXF 取代。

CXF 与 Axis2

CXF Axis2
改进

CXF 是目前使用最多的框架。

它在 Axis2 上进行了很多改进

Axis2 正在逐渐被 CXF 取代。

与 CXF 相比,它需要更多代码

所需代码

与 Axis2 相比,CXF 需要更少的代码

Axis2 相对需要更多代码

标准合规性

CSF 完全符合 JAX-RS 和 JAX-WS

Axis2 不完全符合 JAX-RS 和 JAX-WS

与 Spring 兼容

前端分离

将前端与 JAX-WS 代码清晰分离

没有提供清晰的分离

SOAP

SOAP 代表简单对象访问协议。它是一种用于在两个系统之间通过 Web 服务交换结构化信息的协议。它主要依赖于 XML 来构建数据,并使用 HTTP 或 SMTP 进行消息协商和传输。

有两种方法可以开发 SOAP Web 服务:

  • 代码优先 - 在这种方法中,WSDL 从代码生成。

  • 契约优先 - 在契约优先中,代码从 WSDL 生成。

使用 CXF 进行 SOAP 开发

配置 Maven

将以下配置文件添加到 Maven 的 settings.xml 中。

<profiles>
   <profile>
      <id>Jboss-Fuse</id>
		
      <activation>
         <activeByDefault>true</activeByDefault>
      </activation>
		
      <repositories>
         <repository>
            <id>fusesource</id>
            <url>http://repo.fusesource.com/nexus/content/groups/public/</url>
            <snapshots>
               <enabled>false</enabled>
            </snapshots>
            <releases>
               <enabled>true</enabled>
            </releases>
         </repository>
      </repositories>
		
   </profile>
</profiles>

创建骨架

mvn archetype:generate
-DarchetypeGroupId = org.apache.servicemix.tooling 
-DarchetypeArtifactId = servicemix-cxf-code-first-osgi-bundle 
-DarchetypeVersion=2012.01.0.redhat-60024 
-DgroupId = org.fusesource.example 
-DartifactId = cxf-basic 
-Dversion = 1.0-SNAPSHOT

构建 Web 服务项目.

mvn clean install

使用以下命令将 Web 服务安装到 Fuse 中.

JBossFuse:karaf@root>install -s mvn:org.fusesource.example/cxf-basic/1.0-SNAPSH

检查捆绑包是否已注册 SOQP Web 服务

打开 URL https://:8181/cxf

SOQP web-service

Web 服务应按如下方式列出。

测试 Web 服务

mvn -Pclient

信息 - 从类 com.to 创建服务 {http://ws.totorials.com/} PersonService

torials.ws.Person
Invoking getPerson...
getPerson._getPerson_personId = Guillaume
getPerson._getPerson_ssn = 000-000-0000
getPerson._getPerson_name = Guillaume
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.668 s
[INFO] Finished at: 2016-02-15T21:01:20+05:30
[INFO] Final Memory: 10M/37M
[INFO] ------------------------------------------------------------------------
广告

© . All rights reserved.