CXF教程

CXF 与 POJO

在本章中,您将学习如何开发一个向用户发送问候消息的简单 Web 应用程序。 Web 服务项目使用 WSDL 模型。 CXF 允许您通过提供一个简单的前端将 Apache CXF API 映射到底层 WSDL 来隐藏此 WSDL 模型。
在这个最简单的项目中,Web 服务的接口将直接暴露给客户端,客户端将使用本机 Apache CXF API 来调用 Web 服务。
首先,我们将创建一个网络服务。每个服务都有一个暴露给客户端的接口。我们可以将此接口编写为简单的 Apache CXF 接口或 WSDL 文档。在这种 Apache CXF-First 方法中,我们将通过 Apache CXF 接口公开我们的服务。

开发网络服务

我们将在网络上创建的服务将有一个名为 greetings 的网络方法。该方法采用 string 类型参数,我们将在其中发送用户名。该服务将向呼叫者发送一条问候消息,并将收到的用户名包含在消息中。

网络服务接口

为了公开我们的 Web 服务的接口,我们将创建一个 Apache CXF 接口,如下所示-
//HelloWorld.java
package com.lidihuo.cxf.pojo;
public interface HelloWorld {
   String greetings(String text);
}
该接口只有一种方法叫做 greetings。服务器将实现此接口。在我们的简单应用程序中,这个接口直接暴露给客户端。通常,在 Web 服务应用程序中,您使用 WSDL 来描述 Web 服务接口。在这个简单的应用程序中,我们将向客户端开发人员提供这个直接接口。然后客户端将调用服务器对象上的 greetings 消息。所以首先让我们创建 Web 服务。

Web 服务实现

HelloWorld 接口在 HelloWorldImpl Apache CXF 类中实现,如下所示-
//HelloWorldImpl.java
package com.lidihuo.cxf.pojo;
public class HelloWorldImpl implements HelloWorld {
   @Override
   public String greetings(String text) {
      return "Hi " + text;
   }
}
greetings 方法接收 string 类型的参数,将其附加到问候消息并将结果字符串返回给调用者。
接下来,我们编写服务器应用程序来托管 HelloWorld 服务。

创建服务器

服务器应用程序由两部分组成-
第一部分为我们的网络服务创建一个工厂,以及 第二部分编写了一个 main 方法来实例化它。
服务器使用 ServerFactoryBean 类由 CXF 库提供,用于向远程客户端公开我们的 HelloWorld 接口。因此,我们首先实例化 ServerFactoryBean 类,然后设置它的各种属性-
ServerFactoryBean factory = new ServerFactoryBean();
我们通过调用 factory 对象上的 setServiceClass 方法来设置要调用的服务类-
factory.setServiceClass(HelloWorld.class);
我们通过调用工厂的 setAddress 方法来设置调用我们服务的 URL。请注意,该服务将在此 URL 上发布。
factory.setAddress("http://localhost:5000/Hello");
在这种情况下,该服务部署在嵌入式服务器上,并将侦听端口 5000。您可以选择任何端口号。
在创建工厂之前,需要告诉工厂我们的服务实现类。这是通过调用 factory 对象上的 setServiceBean 方法来完成的,如下所示-
factory.setServiceBean(new HelloWorldImpl());
服务 bean 设置为我们的服务实现类的实例。最后,我们通过调用它的 create 方法来创建工厂-
factory.create();
现在,由于我们已经开发了工厂来运行我们的 Web 服务,接下来我们将编写一个 main 方法来实例化它并使其运行一段时间。
现在,编写一个 main 方法来实例化 HelloServer 类,如下所示-
public static void main(String[] args) throws Exception {
   new HelloServer();
   System.out.println("Listening on port 5000 ...");
}
一旦实例化, HelloServer 类将无限期地继续运行。对于生产部署,您肯定会让您的服务器永远运行。在当前情况下,我们将在预定时间后终止服务器,如下所示-
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting ...");
System.exit(0);
HelloServer 类的完整代码如下-
//HelloServer.java
//HelloServer.java
package com.lidihuo.cxf.pojo;
import org.apache.cxf.frontend.ServerFactoryBean;
public class HelloServer {
   protected HelloServer() throws Exception {
      ServerFactoryBean factory = new ServerFactoryBean();
      factory.setServiceClass(HelloWorld.class);
      factory.setAddress("http://localhost:5000/Hello");
      factory.setServiceBean(new HelloWorldImpl());
      factory.create();
   }
   public static void main(String[] args) throws Exception {
      new HelloServer();
      System.out.println("Listening on port 5000 ...");
      Thread.sleep(5 * 60 * 1000);
      System.out.println("Server exiting ...");
      System.exit(0);
   }
}
我们创建的服务器应用程序使用来自 CXF 库的 ServerFactoryBean 类。我们现在必须在我们的项目中包含这些库才能成功编译 HelloServer 类。我们将使用 Maven 来设置项目依赖项。

设置 Maven 项目

要创建 Maven 项目,请在命令行窗口中键入以下命令。请注意,我们已经在 Mac 机器上对此进行了测试。对于 Windows 和 Linux 安装,说明可能在几个地方有所不同。
mvn archetype:generate
当询问属性时,输入以下值-
Define value for property 'groupId': : com.lidihuo
Define value for property 'artifactId': : cxf-pojo
Define value for property 'version': 1.0-SNAPSHOT: : 1.0
Define value for property 'package': com.lidihuo: : com.lidihuo.cxf.pojo
在 maven 命令完成后,您会发现在当前文件夹中创建的相应文件夹结构以及 pom.xml 文件。
生成的目录结构如下所示-
目录结构
您将在 pom.xml 中添加 CXF 依赖项,并将上面创建的 Apache CXF 文件复制到 maven 创建的结构的适当文件夹中。为方便您参考,我们在下面给出了我们在机器上创建的项目的 pom.xml 文件。
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.lidihuo</groupId>
   <artifactId>cxf-pojo</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
   
   <profiles>
      <profile>
         <id>server</id>
         <build>
            <defaultGoal>test</defaultGoal>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <executions>
                     <execution>
                        <phase>test</phase>
                        <goals>
                           <goal>java</goal>
                        </goals>
                        <configuration>
                           <mainClass>
                              com.lidihuo.cxf.pojo.HelloServer
                           </mainClass>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
            </plugins>
         </build>
      </profile>
      
      <profile>
         <id>client</id>
         <build>
            <defaultGoal>test</defaultGoal>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <executions>
                     <execution>
                        <phase>test</phase>
                        <goals>
                           <goal>java</goal>
                        </goals>
                        <configuration>
                           <mainClass>
                           com.lidihuo.cxf.pojo.HelloClient
                           </mainClass>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
            </plugins>
         </build>
      </profile>
   </profiles>
   <dependencies>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-features-logging</artifactId>
         <version>3.3.0</version>
         <type>jar</type>
      </dependency>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-simple</artifactId>
         <version>3.3.0</version>
         <type>jar</type>
      </dependency>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-transports-http</artifactId>
         <version>3.3.0</version>
      </dependency>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>3.3.0</version>
      </dependency>
      <!--Jetty is needed if you're using the CXFServlet-->
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-transports-http-jetty</artifactId>
         <version>3.3.0</version>
      </dependency>
   </dependencies>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
   </properties>
</project>
上面的 pom.xml 可能包含额外的依赖项,这些依赖项与此项目无关,但本教程中的下一个项目需要这些依赖项。无论如何,包含额外的依赖项并没有什么坏处。

项目文件夹结构

放置服务器和客户端 Apache CXF 文件后我机器上的项目文件夹结构如下所示,以供您快速参考-
项目文件夹结构

运行服务器

要构建项目,请在命令行窗口中使用以下命令-
mvn clean install
您可以使用以下命令启动服务器-
mvn-Pserver
这将启动服务器,您将在控制台上看到以下提示-
INFO: Creating Service {http://pojo.cxf.lidihuo.com/}HelloWorld from class com.lidihuo.cxf.pojo.HelloWorld
INFO: Setting the server's publish address to be http://localhost:5000/Hello
Listening on port 5000 ...
现在,在您的浏览器窗口中指定我们发布的服务的 URL。您将看到以下输出-
输出文档树
这确认我们的服务正在本地主机上的指定端口上运行。由于我们没有在调用中指定 greetings 消息,因此会向浏览器返回 SOAP 错误消息。
您可以使用您选择的 SOAP 客户端进一步测试您的网络服务。这里我们使用了 Postman 来测试我们的服务器。
输出如下所示-
运行服务器输出
注意 SOAP 请求 是手工编码的。发布请求后,服务器发送了一个 SOAP 响应 消息,该消息显示在屏幕截图的底部。
由此,您可以理解 CXF 保持对请求和响应使用 SOAP 协议,同时为您提供对当今世界确实存在的各种网络技术的统一视图。这极大地简化了 Web 应用程序的开发。
我们的下一个任务是创建一个客户端,该客户端将使用您创建的 Web 服务。

创建客户端

在服务器应用程序中, HelloWorld 是公开我们的 Web 服务的接口。 Web 服务本身只向客户端提供一条简单的问候消息。通常,Web 服务接口使用 WSDL(Web 服务描述语言)向外界公开。在这个简单的应用程序中,我们将通过直接公开服务接口,即 HelloWorld.class 来向客户端公开我们的 Web 服务。
为此,CXF 提供了一个名为 ClientProxyFactoryBean 的工厂类,它允许我们将所需的接口附加到创建的工厂实例。
首先,我们创建一个工厂 bean 实例如下-
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
我们在工厂 bean 实例上调用 setAddress 方法来设置可以调用我们的 Web 服务的 URL。在我们的例子中,我们将使用在前面步骤中创建服务器时使用的 URL-
factory.setAddress("http://localhost:5000/Hello");
接下来,我们在 factory 实例上调用 create 方法,将我们的服务接口 HelloWorld.class 附加到它。
HelloWorld helloServer = factory.create(HelloWorld.class);
最后,我们调用 greetings 方法来调用远程 Web 服务。
System.out.println(helloServer.greetings(System.getProperty("user.name")));
这将在您的控制台上打印一条问候消息。
客户端应用程序的整个源代码如下所示-
//HelloClient.java
package com.lidihuo.cxf.pojo;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
public class HelloClient {
   public static void main(String[] args) throws Exception {
      ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
      factory.setAddress("http://localhost:5000/Hello");
      HelloWorld helloServer = factory.create(HelloWorld.class);
      System.out.println(helloServer.greetings(System.getProperty("user.name")));
   }
}

运行客户端

确保服务器仍在您的机器上运行。如果超时,请使用以下命令重新启动服务器-
mvn-Pserver
您将在控制台上看到以下消息-
Listening on port 5000 ...
现在,在我们设置为 5 分钟的服务器超时之前,打开另一个命令行窗口并使用以下命令启动客户端-
mvn-Pclient
您将在命令行上看到类似于以下内容的消息-
Hi lidihuo
请注意, lidihuo 是我们的用户名。您将收到带有您自己名字的问候。
在下一章中,我们将学习如何在 JAX-WS(用于 XML Web 服务的 Apache CXF API)项目中使用 CXF。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4