Java Jersey REST APIs
As an API developer, use this guide to onboard your Java Jersey REST API service with the Zoweâ„¢ API Mediation Layer. This article outlines a step-by-step process to make your API service available in the API Mediation Layer.
Note: The following guide is to onboard a REST API service using the API ML/enabler version 1.2 and earlier. To onboard a REST API service using API ML/enabler version 1.3 and higher, see the Onboarding Overview for a complete list of Zoweâ„¢ API Mediation Layer onboarding methods.
The following procedure is an overview of steps to onboard a Java Jersey REST API application with the API Mediation Layer.
Follow these steps:
#
Get enablers from the ArtifactoryThe first step to onboard a Java Jersey REST API into the Zowe ecosystem is to get enabler annotations from the Artifactory. Enablers prepare your service for discovery and for the retrieval of Swagger documentation.
You can use either Gradle or Maven build automation systems.
#
Gradle guideUse the following procedure if you use Gradle as your build automation system.
Tip: To migrate from Maven to Gradle, go to your project directory and run gradle init
. This converts the Maven build to a Gradle build by generating a setting.gradle
file and a build.gradle
file.
Follow these steps:
Create a
gradle.properties
file in the root of your project.In the
gradle.properties
file, set the following URL of the repository and customize the values of your credentials to access the repository.This file specifies the URL for the repository of the Artifactory. The enabler-jersey artifact is downloaded from this repository.
Add the following Gradle code block to the
build.gradle
file:The
ext
object declares themavenRepository
property. This property is used as the project repository.In the same
build.gradle
file, add the following code to the dependencies code block. This snippet adds the enabler-jersey artifact as a dependency of your project:In your project directory, run the
gradle build
command to build your project.
#
Maven guideUse the following procedure if you use Maven as your build automation system.
Tip: To migrate from Gradle to Maven, go to your project directory and run gradle install
. This command automatically generates a pom-default.xml
inside the build/poms
subfolder where all of the dependencies are contained.
Follow these steps:
Add the following xml tags within the newly created
pom.xml
file:This file specifies the URL for the repository of the Artifactory where you download the enabler-jersey artifact.
In the same file, copy the following xml tags to add the enabler-jersey artifact as a dependency of your project:
Create a
settings.xml
file and copy the following xml code block which defines the credentials for the Artifactory:Copy the
settings.xml
file inside the${user.home}/.m2/
directory.In the directory of your project, run the
mvn package
command to build the project.
#
Add API ML Onboarding ConfigurationAs an API service developer, you set multiple configuration settings in your application.yml
that correspond to the API ML. These settings enable an API to be discoverable and included in the API Catalog. Some of the settings in the application.yml
are internal and are set by the API service developer. Some settings are externalized and set by the customer system administrator. Those external settings are service parameters and are in the format: ${environment.*}
.
Important! Spring Boot configuration can be externalized in multiple ways. For more information see: Externalized configuration. This Zowe onboarding documentation applies to API services that use an application.yml file for configuration. If your service uses a different configuration option, transform the provided configuration sample to the format that your API service uses.
Tip: For information about how to set your configuration when running a Spring Boot application under an external servlet container (TomCat), see the following short stackoverflow article: External configuration for spring-boot application.
Follow these steps:
Add the following
#MFAAS configuration section
in yourapplication.yml
:In order to run your application locally, you need to define variables used under the
environment
group.Important: Add this configuration also to the
application.yml
used for testing. Failure to add this configuration to theapplication.yml
will cause your tests to fail.Change the MFaaS parameters to correspond with your API service specifications. Most of these internal parameters contain "your service" text.
Note:
${mfaas.*}
variables are used throughout theapplication.yml
sample to reduce the number of required changes.Tip: When existing parameters set by the system administrator are already present in your configuration file (for example,
hostname, address, contextPath,
andport
), we recommend that you replace them with the corresponding MFaaS properties.a. Discovery Parameters
mfaas.discovery.serviceId
This parameter specifies the service instance identifier to register in the API ML installation. The service ID is used in the URL for routing to the API service through the Gateway. The service ID uniquely identifies instances of a microservice in the API ML. The system administrator at the customer site defines this parameter.
Important! Ensure that the service ID is set properly with the following considerations:
When two API services use the same service ID, the API Gateway considers the services to be clones. An incoming API request can be routed to either of them.
The same service ID should be set for only multiple API service instances for API scalability.
The service ID value must contain only lowercase alphanumeric characters.
The service ID cannot contain more than 40 characters.
The service ID is linked to security resources. Changes to the service ID require an update of security resources.
The service ID must match the
spring.application.name
parameter.Examples:
If the customer system administrator sets the service ID to
sysviewlpr1
, the API URL in the API Gateway appears as the following URL:If the customer system administrator sets the service ID to vantageprod1, the API URL in the API Gateway appears as the following URL:
mfaas.discovery.locations
Specifies the public URL of the Discovery Service. The system administrator at the customer site defines this parameter.
Example:
mfaas.discovery.enabled
This parameter specifies whether the API service instance is to be discovered in the API ML. The system administrator at the customer site defines this parameter. Set this parameter to
true
if the API ML is installed and configured. Otherwise, you can set this parameter tofalse
to exclude an API service instances from the API ML.mfaas.discovery.fetchRegistry
This parameter specifies whether the API service is to receive regular update notifications from the discovery service. Under most circumstances, you can accept the default value of
false
for the parameter.mfaas.discovery.region
b. Service and Server Parameters
mfaas.service.hostname
This parameter specifies the hostname of the system where the API service instance runs. This parameter is externalized and is set by the customer system administrator. The administrator ensures the hostname can be resolved by DSN to the IP address that is accessible by applications running on their z/OS systems.
mfaas.service.ipAddress
This parameter specifies the local IP address of the system where the API service instance runs. This IP address may or may not be a public IP address. This parameter is externalized and set by the customer system administrator.
mfaas.server.scheme
This parameter specifies whether the API service is using the HTTPS protocol. This value can be set to
https
orhttp
depending on whether your service is using SSL.mfaas.server.port
This parameter specifies the port that is used by the API service instance. This parameter is externalized and set by the customer system administrator.
mfaas.server.contextPath
This parameter specifies the prefix that is used within your API service URL path.
Examples:
- If your API service does not use an extra prefix in the URL (for example,
http://host:port/endpoint1/
), set this value to/
. - If your API service uses an extra URL prefix set the parameter to that prefix value.
For the URL:
http://host:port/filemaster/endpoint1/
, set this parameter to/filemaster
. - In both examples, the API service URL appears as the following URL when routed through the Gateway:
c. API Catalog Parameters
These parameters are used to populate the API Catalog. The API Catalog contains information about every registered API service. The Catalog also groups related APIs. Each API group has its own name and description. Catalog groups are constructed in real-time based on information that is provided by the API services. Each group is displayed as a tile in the API Catalog UI dashboard.
mfaas.catalog-ui-tile.id
This parameter specifies the unique identifier for the API services product family. This is the grouping value used by the API ML to group multiple API services together into "tiles". Each unique identifier represents a single API Catalog UI dashboard tile. Specify a value that does not interfere with API services from other products.
mfaas.catalog-ui-tile.title
This parameter specifies the title of the API services product family. This value is displayed in the API Catalog UI dashboard as the tile title
mfaas.catalog-ui-tile.description
This parameter specifies the detailed description of the API services product family. This value is displayed in the API Catalog UI dashboard as the tile description
mfaas.catalog-ui-tile.version
This parameter specifies the semantic version of this API Catalog tile. Increase the version when you introduce new changes to the API services product family details (title and description).
mfaas.discovery.info.serviceTitle
This parameter specifies the human readable name of the API service instance (for example, "Endevor Prod" or "Sysview LPAR1"). This value is displayed in the API Catalog when a specific API service instance is selected. This parameter is externalized and set by the customer system administrator.
Tip: We recommend that you provide a good default value or give good naming examples to the customers.
mfaas.discovery.info.description
This parameter is a short description of the API service.
Example: "
CA Endevor SCM - Production Instance
" or "CA SYSVIEW running on LPAR1
". This value is displayed in the API Catalog when a specific API service instance is selected. This parameter is externalized and set by the customer system administrator.Tip: We recommend that you provide a good default value or give good naming examples to the customers. Describe the service so that the end user knows the function of the service.
mfaas.discovery.info.swaggerLocation
This parameter specifies the location of a static swagger document. The JSON document contained in this file is displayed instead of the automatically generated API documentation. The JSON file must contain a valid OpenAPI 2.x Specification document. This value is optional and commented out by default.
Note: Specifying a
swaggerLocation
value disables the automated JSON API documentation generation with the SpringFox library. By disabling auto-generation, you need to keep the contents of the manual swagger definition consistent with your endpoints. We recommend to use auto-generation to prevent incorrect endpoint definitions in the static swagger documentation.
d. Metadata Parameters
The routing rules can be modified with parameters in the metadata configuration code block.
Note: If your REST API does not conform to Zowe API Mediation layer REST API Building codes, configure routing to transform your actual endpoints (serviceUrl) to gatewayUrl format. For more information see: REST API Building Codes
eureka.instance.metadata-map.routed-services.<prefix>
This parameter specifies a name for routing rules group. This parameter is only for logical grouping of further parameters. You can specify an arbitrary value but it is a good development practice to mention the group purpose in the name.
Examples:
eureka.instance.metadata-map.routed-services.<prefix>.gatewayUrl
Both gateway-url and service-url parameters specify how the API service endpoints are mapped to the API gateway endpoints. The gateway-url parameter sets the target endpoint on the gateway.
metadata-map.routed-services.<prefix>.serviceUrl
Both gateway-url and service-url parameters specify how the API service endpoints are mapped to the API gateway endpoints. The service-url parameter points to the target endpoint on the gateway.
eureka.instance.metadata-map.apiml.apiInfo.apiId
This parameter specifies the API identifier that is registered in the API Mediation Layer installation. The API ID uniquely identifies the API in the API Mediation Layer. The same API can be provided by multiple services. The API ID can be used to locate the same APIs that are provided by different services. The creator of the API defines this ID. The API ID needs to be a string of up to 64 characters that uses lowercase alphanumeric characters and a dot:
.
.We recommend that you use your organization as the prefix.
eureka.instance.metadata-map.apiml.apiInfo.gatewayUrl
The base path at the API gateway where the API is available. Ensure that it is the same path as the
gatewayUrl
value in theroutes
sections.eureka.instance.metadata-map.apiml.apiInfo.documentationUrl
(Optional) This parameter specifies a link to external documentation, if needed. The link to the external documentation can be included along with the Swagger documentation.
eureka.instance.metadata-map.apiml.apiInfo.swaggerUrl
(Optional) This parameter specifies the HTTP or HTTPS address where the Swagger JSON document is available.
Important! Ensure that each of the values for the
gatewayUrl
parameter are unique in the configuration. DuplicategatewayUrl
values may cause requests to be routed to the wrong service URL.Note: The endpoint
/api-doc
returns the API service Swagger JSON. This endpoint is introduced by the@EnableMfaasInfo
annotation and is utilized by the API Catalog.
e. Swagger Api-Doc Parameters
This parameter configures API Version Header Information, specifically the InfoObject section, and adjusts Swagger documentation that your API service returns. Use the following format:
The following parameters describe the function of the specific version of an API. This information is included in the swagger JSON and displayed in the API Catalog:
where:
- If your API service does not use an extra prefix in the URL (for example,
v1
Specifies the major version of your service API:
v1, v2,
etc.title
Specifies the title of your service API.
description
Specifies the high-level function description of your service API.
version
Specifies the actual version of the API in semantic format.
basePackage
Specifies the package where the API is located. This option only exposes endpoints that are defined in a specified java package. The parameters
basePackage
andapiPattern
are mutually exclusive. Specify only one of them and remove or comment out the second one.apiPattern
This option exposes any endpoints that match a specified regular expression. The parameters
basePackage
andapiPattern
are mutually exclusive. Specify just one of them and remove or comment out the second one.Tip: You have three options to make your endpoints discoverable and exposed:
basePackage
,apiPattern
, or none (if you do not specify a parameter). IfbasePackage
orapiPattern
are not defined, all endpoints in the Spring Boot app are exposed.
#
Setup keystore with the service certificateTo register with the API Mediation Layer, a service is required to have a certificate that is trusted by API Mediation Layer.
Follow these steps:
Follow instructions at Generating certificate for a new service on localhost.
When a service is running on
localhost
, the command can have the following format:Alternatively, for the purpose of local development, copy or use the
<api-layer-repository>/keystore/localhost.truststore.p12
in your service without generating a new certificate.Update the configuration of your service
application.yml
to contain the HTTPS configuration by adding the following code:
Note: You need to define both keystore and truststore even if your server is not using an HTTPS port.
#
Externalize parametersIn order to externalize parameters, you have to create a ServletContextListener
. To create your own
ServletContextListener
, register a ServletContextListener
and enable it to read all
the properties defined in the YAML file.
Follow these steps:
Define parameters that you want to externalize in a YAML file. Ensure that this file is placed in the
WEB-INF
folder located in the module of your service. Check theApiMediationServiceConfig.java
class insidecom.ca.mfaas.eurekaservice.client.config
package in theintegration-enabler-java
to see the mapped parameters and make sure that the YAML file follows the correct structure. The following example shows the structure of the YAML file:Before the web application (Tomcat) is started, create a
ServletContextListener
to run the defined code.Example:
Register the listener. Use one of the following two options:
Add the
@WebListener
annotation to the servlet.Reference the listener by adding the following code block to the deployment descriptor
web.xml
.Example:
#
Download Apache Tomcat and enable SSLTo run Helloworld Jersey, requires the installation of Apache Tomcat. As the service uses HTTPS, configure Tomcat to use the SSL/TLS protocol.
Follow these steps:
Download Apache Tomcat 8.0.39 and install it.
Build Helloworld Jersey through IntelliJ or by running
gradlew helloworld-jersey:build
in the terminal.Enable HTTPS for Apache Tomcat with the following steps:
a) Go to the
apache-tomcat-8.0.39-windows-x64\conf
directory.Note: The full path depends on where you decided to install Tomcat.
b) Open the
server.xml
file with a text editor as Administrator and add the following xml block:Ensure to comment the HTTP connector which uses the same port. c) Navigate to the
WEB-INF/
located inhelloworld-jersey
module and add the following xml block to theweb.xml
file. This should be added right below the<servlet-mapping>
tag:
#
Run your serviceAfter you externalize the parameters to make them readable through Tomcat and enable SSL, you are ready to run your service in the API ML Ecosystem.
Note: The following procedure uses localhost
testing.
Follow these steps:
Run the following services to onboard your application:
Tip: For more information about how to run the API Mediation Layer locally, see Running the API Mediation Layer on Local Machine.
- Gateway Service
- Discovery Service
- API Catalog Service
Run
gradlew tomcatRun
with these additional parameters:To provide additional information about SSL configuration status while deploying, use the following parameter:
-Djavax.net.debug=SSL
Tip: Wait for the services to be ready. This process may take a few minutes.
Navigate to the following URL:
Enter
eureka
as a username andpassword
as a password and check if the service is registered to the Discovery Service.Go to the following URL to reach the API Catalog through the Gateway (
port 10010
) and check if the API documentation of the service is retrieved:You successfully onboarded your Java Jersey application if your service is running and you can access the API documentation.