带有 POJO 的 Apache CXF


在本章中,你将学习如何开发一个向用户发送问候消息的简单 Web 应用程序。一个 Web 服务项目使用 WSDL 模型。 CXF 允许你通过提供一个简单的前端将 Apache CXF API 映射到底层 WSDL 来隐藏此 WSDL 模型。

在这个最简单的项目中,Web 服务的接口将直接暴露给客户端,客户端将使用本机 Apache CXF API 来调用 Web 服务。

首先,我们将创建一个 Web 服务。每个服务都有一个向客户端公开的接口。我们可以将此接口编写为简单的 Apache CXF 接口或 WSDL 文档。在这种 Apache CXF-First 方法中,我们将通过 Apache CXF 接口公开我们的服务。

开发 Web 服务


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

网络服务接口


为了暴露我们的 Web 服务的接口,我们将创建一个 Apache CXF 接口,如下所示:

// HelloWorld.java
package com.newbiego.cxf.pojo;
public interface HelloWorld {
    String greetings(String text);
}

该接口只有一种方法称为 问候 .服务器将实现这个接口。在我们简单的应用程序中,这个接口直接暴露给客户端。通常,在 Web 服务应用程序中,你使用 WSDL 来描述 Web 服务接口。在这个简单的应用程序中,我们将向客户端开发人员提供这个直接接口。然后客户将调用 问候 服务器对象上的消息。因此,首先让我们创建 Web 服务。

网络服务实现


The 你好世界 interface is implemented in the HelloWorldImpl Apache CXF类如下图:

// HelloWorldImpl.java
package com.newbiego.cxf.pojo;
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String greetings(String text) {
        return "Hi " + text;
    }
}

The 问候 方法接收一个参数 string 类型,将其附加到问候消息并将结果字符串返回给调用者。

接下来,我们编写服务器应用程序来托管 你好世界 service.

创建服务器


服务器应用程序由两部分组成:

  • 第一部分为我们的 Web 服务创建一个工厂,然后

  • 第二部分写了一个 main 实例化它的方法。

服务器使用 服务器工厂Bean CXF 库提供的类来暴露我们的 你好世界 interface to remote clients. Thus, we first instantiate the 服务器工厂Bean 类然后设置它的各种属性:

ServerFactoryBean factory = new ServerFactoryBean();

我们通过调用 设置服务类 上的方法 factory object:

factory.setServiceClass(HelloWorld.class);

我们通过调用工厂的来设置调用我们服务的 URL 设置地址 方法。请注意,该服务将在此 URL 上发布。

factory.setAddress("http:// 本地主机:5000/Hello");

在这种情况下,该服务部署在嵌入式服务器上,并将监听端口 5000。你可以选择你选择的任何端口号。

在创建工厂之前,你需要告诉工厂我们的服务实现类。这是通过调用 setServiceBean 上的方法 factory 对象如下图:

factory.setServiceBean(new HelloWorldImpl());

服务 bean 设置为我们的服务实现类的实例。最后,我们通过调用它来创建工厂 create method:

factory.create();

现在,由于我们已经开发了工厂来运行我们的 Web 服务,接下来我们将编写一个 main 方法来实例化它并让它运行一段时间。

现在,写一个 main 实例化的方法 你好服务器 类如下:

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);

整个代码 你好服务器 类如下:

// HelloServer.java
// HelloServer.java
package com.newbiego.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:// 本地主机: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);
    }
}

我们创建的服务器应用程序使用 服务器工厂Bean 来自 CXF 库的类。我们现在必须在我们的项目中包含这些库才能成功编译 你好服务器 班级。我们将使用 Maven 设置项目依赖项。

设置 Maven 项目


要创建 Maven 项目,请在命令行窗口中键入以下命令。请注意,我们已经在 Mac 机器上对此进行了测试。对于 Windows 和 Linux 安装,说明可能在几个地方有所不同。

mvn archetype:generate

当询问属性时,输入以下值:

Define value for property 'groupId': : com.newbiego
Define value for property 'artifactId': : cxf-pojo
Define value for property 'version': 1.0-SNAPSHOT: : 1.0
Define value for property 'package': com.newbiego: : com.newbiego.cxf.pojo

完成 maven 命令后,你将找到在当前文件夹中创建的适当文件夹结构以及 pom.xml 文件。

生成的目录结构如下图:

Directory Structure

你将在 pom.xml 并将上面创建的 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:
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.newbiego</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.newbiego.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.newbiego.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文件后,我机器上的项目文件夹结构如下图,供大家快速参考:

Project Folder Structure

运行服务器


要构建项目,请在命令行窗口中使用以下命令:

mvn clean install

你可以使用以下命令启动服务器:

mvn -Pserver

这将启动服务器,你将在控制台上看到以下提示:

INFO: Creating Service {http:// pojo.cxf.newbiego.com/}来自类 com.newbiego.cxf.pojo.HelloWorld 的 HelloWorld
INFO: Setting the server's publish address to be http:// 本地主机:5000/你好
Listening on port 5000 ...

现在,在你的浏览器窗口中指定我们已发布服务的 URL。你将看到以下输出:

输出 Document Tree

这确认我们的服务正在本地主机上的指定端口上运行。由于我们没有指定 问候 在我们调用消息时,会向浏览器返回一个 SOAP 错误消息。

你可以使用你选择的 SOAP 客户端进一步测试你的 Web 服务。这里我们使用了 Postman 测试我们的服务器。

输出如下所示:

Running Server 输出

请注意 SOAP 请求 是手工编码的。发布请求后,服务器发送了一个 SOAP 响应 消息,显示在屏幕截图的底部。

由此,你可以理解 CXF 保持对请求和响应的 SOAP 协议的使用,同时为你提供对当今世界中确实存在的各种 Web 技术的统一视图。这极大地简化了 Web 应用程序的开发。

我们的下一个任务是创建一个将使用你创建的 Web 服务的客户端。

创建客户端


在服务器应用程序中 你好世界 是公开我们的 Web 服务的接口。 Web 服务本身只是向客户端提供一个简单的问候消息。通常,Web 服务接口使用 WSDL(Web 服务描述语言)向外界公开。在这个简单的应用程序中,我们将通过直接暴露服务接口将我们的 Web 服务暴露给客户端,这就是 HelloWorld.class .

为此,CXF 提供了一个工厂类,称为 ClientProxyFactoryBean 这允许我们将所需的接口附加到创建的工厂实例。

首先,我们创建一个工厂bean实例如下:

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();

我们称 设置地址 工厂 bean 实例上的方法来设置可以调用我们的 Web 服务的 URL。在我们的例子中,我们将使用上一步创建服务器时使用的 URL:

factory.setAddress("http:// 本地主机:5000/Hello");

接下来,我们调用 create 上的方法 factory 附加我们的服务接口的实例 HelloWorld.class to it.

HelloWorld helloServer = factory.create(HelloWorld.class);

最后,我们称 问候 方法来调用远程 Web 服务。

System.out.println(helloServer.greetings(System.getProperty("user.name")));

这将在你的控制台上打印一条问候消息。

客户端应用程序的完整源码如下图所示:

// HelloClient.java
package com.newbiego.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:// 本地主机: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 newbiego

注意 教程点 是我们的用户名。你将收到带有你自己名字的问候。

在下一章中,我们将学习如何在 JAX-WS(Apache CXF API for XML Web Services)项目中使用 CXF。