此内容没有您所选择的语言版本。
Chapter 3. Implement, build, and deploy a Fuse application based on a REST API
You can use Red Hat Fuse Apicurito to generate a Camel Fuse project based on a REST API definition. In your Fuse development environment, you can complete the Camel routes and Rest DSL API. Finally, you can build the project and deploy the resulting application to Fuse on OpenShift.
Prerequisites
-
You have an existing API definition, which complies with the OpenAPI 2.0 specification. For example, an
openapi-spec.json
file that you created with Apicurito. - Apicurito is installed and running on your local OpenShift cluster.
- You have an existing OpenShift project with Apicurito added as a service.
- You have installed Maven and Red Hat Fuse.
The following topics describe how to implement, build, and deploy a Fuse application based on a REST API:
3.1. Uploading an API definition to Apicurito
You can upload an existing API definition to Apicurito.
Prerequisites
-
You have an existing API definition, which complies with the OpenAPI 2.0 specification. For example, an
openapi.json
file that you created with Apicurito. - Apicurito is installed and running on your local OpenShift cluster.
- You have an existing OpenShift project with Apicurito added as an application.
Procedure
- In your OpenShift web console, open the project that contains Apicurito.
Open the Apicurito console. In the list of applications for the project, click the URL under apicurito. For example:
https://apicurito-myproject.192.168.64.38.nip.io
The Apicurito console opens in a separate web browser tab or window:
Click Open API.
A file manager window opens.
In the file manager window:
-
Navigate to the folder that contains the existing OpenAPI definition file, for example,
openapi.json
. Select the OpenAPI definition file and then click Open.
The OpenAPI definition opens in the Apicurito console. For example:
-
Navigate to the folder that contains the existing OpenAPI definition file, for example,
3.2. Generating a Fuse Camel project from Apicurito
You can use Apicurito to generate a Fuse Camel project based on an API definition.
Prerequisites
- Apicurito is installed and running on your local OpenShift cluster.
- You have an existing OpenShift project with Apicurito added as an application.
- You have created or opened an API definition file in the Apicurito console.
Procedure
In the Apicurito console:
- Click Generate.
- Select Fuse Camel Project from the drop-down list.
Apicurito generates a camel-project.zip
file and downloads it to your local default download folder.
The zip file contains a Fuse Camel project that provides a default skeleton implementation of the API definition using Camel’s Rest DSL and includes all resource operations. The project also includes the original OpenAPI definition file that you used to generate the project.
3.3. Completing the Apicurito-generated Camel project
Apicurito generates a Fuse project that provides a default skeleton implementation of the API definition using Camel’s Rest DSL and covering all resource operations. In your Fuse development environment, you complete the project.
Prerequisites
-
You have a
camel-project.zip
file generated by Apicurito. - (Optional) You have installed Red Hat Developer Studio with Fuse Tooling.
Procedure
-
Unzip the Apicurito-generated
camel-project.zip
file to a temporary folder. - Open Red Hat Developer Studio.
-
In Developer Studio, select File
Import. -
In the Import dialog, select Maven
Existing Maven Projects. -
Open the project’s
camel-context.xml
file in the editor view. Click the REST tab to edit the Rest DSL components.
For information on defining REST services, see the "Defining REST services" section of the Apache Camel Development Guide.
For information on extending JAX-RS endpoints with Swagger support, see the Apache CXF Development Guide.
For information on using the Fuse Tooling REST editor, see the "Viewing and editing Rest DSL components" section of the Tooling User Guide.
In the Design tab, edit the Camel routes.
For information on editing Camel routes, see the "Editing a routing context in the route editor" section of the Tooling User Guide.
3.4. Building and deploying a REST service
After you complete the Fuse project, you can build and deploy the project in OpenShift.
Prerequisites
- You have a complete, error-free Fuse project that defines a REST service.
- You have installed Java 8 JDK (or later) and Maven 3.3.x (or later).
Procedure
If you have a single-node OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, installed and running, you can deploy your project there.
To deploy this project to a running single-node OpenShift cluster:
Log in to your OpenShift cluster:
$ oc login -u developer -p developer
Create a new OpenShift project for the project. For example, the following command creates a new project named test-deploy.
$ oc new-project test-deploy
Change the directory to the folder that contains your Fuse Camel project (for example,
myworkspace/camel-project
) :$ cd myworkspace/camel-project
Build and deploy the project to the OpenShift cluster:
$ mvn clean fabric8:deploy -Popenshift
-
In your browser, open the OpenShift console and navigate to the project (for example, test-deploy). Wait until you can see that the pod for the
camel-project
application has started. -
On the project’s Overview page, locate the URL for the
camel-project
application. The URL uses this form:http://camel-project-MY_PROJECT_NAME.OPENSHIFT_IP_ADDR.nip.io
. - Click the URL to access the service.