guide-microprofile-openapi
guide-microprofile-openapi copied to clipboard
A guide on how to document and filter RESTful APIs from code or static files by using MicroProfile OpenAPI: https://openliberty.io/guides/microprofile-openapi.html
// Copyright (c) 2017, 2024 IBM Corporation and others. // Licensed under Creative Commons Attribution-NoDerivatives // 4.0 International (CC BY-ND 4.0) // https://creativecommons.org/licenses/by-nd/4.0/ // // Contributors: // IBM Corporation :projectid: microprofile-openapi :page-layout: guide-multipane :page-duration: 20 minutes :page-releasedate: 2018-03-16 :page-guide-category: microprofile :page-essential: false :page-description: Explore how to document and filter RESTful APIs from code or static files by using MicroProfile OpenAPI. :page-tags: ['microprofile'] :page-permalink: /guides/{projectid} :page-related-guides: ['cdi-intro', 'microprofile-config'] :common-includes: https://raw.githubusercontent.com/OpenLiberty/guides-common/prod :source-highlighter: prettify :page-seo-title: Documenting RESTful APIs using MicroProfile OpenAPI :page-seo-description: A tutorial on how to document and filter RESTful APIs by using MicroProfile OpenAPI :guide-author: Open Liberty = Documenting RESTful APIs
[.hidden] NOTE: This repository contains the guide documentation source. To view the guide in published form, view it on the https://openliberty.io/guides/{projectid}.html[Open Liberty website].
Explore how to document and filter RESTful APIs from code or static files by using MicroProfile OpenAPI.
:openapi-url: http://localhost:9080/openapi :inv-url: http://localhost:9080/inventory/systems :sys-url: http://localhost:9080/inventory/properties
== What you'll learn
You will learn how to document and filter RESTful APIs from annotations, POJOs, and static OpenAPI files by using MicroProfile OpenAPI.
The OpenAPI specification, previously known as the Swagger specification, defines a standard interface for documenting and exposing RESTful APIs. This specification allows both humans and computers to understand or process the functionalities of services without requiring direct access to underlying source code or documentation. The MicroProfile OpenAPI specification provides a set of Java interfaces and programming models that allow Java developers to natively produce OpenAPI v3 documents from their JAX-RS applications.
You will document the RESTful APIs of the provided inventory service, which serves two endpoints, inventory/systems and inventory/properties. These two endpoints function the same way as in the other MicroProfile guides.
Before you proceed, note that the 1.0 version of the MicroProfile OpenAPI specification does not define how the /openapi endpoint may be partitioned in the event of multiple JAX-RS applications running on the same server. In other words, you must stick to one JAX-RS application per server instance as the behaviour for handling multiple applications is currently undefined.
// ================================================================================================= // Getting Started // ================================================================================================= [role='command'] include::{common-includes}/gitclone.adoc[]
// ================================================================================================= // Try what you'll build // ================================================================================================= [role='command'] include::{common-includes}/twyb-intro.adoc[]
ifndef::cloud-hosted[]
Next, point your browser to the {openapi-url}[{openapi-url}^] URL and you'll see the RESTful APIs of the inventory service. You can also point to the {openapi-url}/ui[{openapi-url}/ui^] URL for a more interactive view of the deployed APIs. This UI is built from the https://swagger.io/tools/swagger-ui/[Open Source Swagger UI^] and renders the generated /openapi document into a very user friendly page.
endif::[]
ifdef::cloud-hosted[] To open a new command-line session, select Terminal > New Terminal from the menu of the IDE.
Next, run the following curl command to see the RESTful APIs of the inventory service:
curl http://localhost:9080/openapi
A UI is also available for a more interactive view of the deployed APIs. Click the following button to visit the UI by the /openapi/ui endpoint. ::startApplication{port="9080" display="external" name="Visit OpenAPI UI" route="/openapi/ui"}
This UI is built from the Open Source Swagger UI, which renders the generated /openapi document into a very user friendly page. endif::[]
[role='command'] include::{common-includes}/twyb-end.adoc[]
// ================================================================================================= // Generating APIs // =================================================================================================
== Generating the OpenAPI document for the inventory service
You can generate an OpenAPI document in various ways. First, because all Jakarta Restful Web Services annotations are processed by default, you can augment your existing Jakarta Restful Web Services annotations with OpenAPI annotations to enrich your APIs with a minimal amount of work. Second, you can use a set of predefined models to manually create all elements of the OpenAPI tree. Finally, you can filter various elements of the OpenAPI tree, changing them to your liking or removing them entirely.
Navigate to the start directory to begin.
// cloud hosted instructions
ifdef::cloud-hosted[]
cd /home/project/guide-microprofile-openapi/start
endif::[]
[role='command'] include::{common-includes}/devmode-lmp33-start.adoc[]
Because the Jakarta Restful Web Services framework handles basic API generation for Jakarta Restful Web Services annotations, a skeleton OpenAPI tree will be generated from the inventory service. You can use this tree as a starting point and augment it with annotations and code to produce a complete OpenAPI document.
ifndef::cloud-hosted[] Now, visit the {openapi-url}[{openapi-url}^] URL to see the generated OpenAPI tree. You can also visit the {openapi-url}/ui[{openapi-url}/ui^] URL for a more interactive view of the APIs. endif::[]
ifdef::cloud-hosted[] Now, run the following curl command to see the generated OpenAPI tree:
curl http://localhost:9080/openapi
Click the following button to visit the UI by the /openapi/ui endpoint for a more interactive view of the APIs. ::startApplication{port="9080" display="external" name="Visit OpenAPI UI" route="/openapi/ui"} endif::[]
=== Augmenting the existing Jakarta Restful Web Services annotations with OpenAPI annotations
Because all Jakarta Restful Web Services annotations are processed by default, you can augment the existing code with OpenAPI annotations without needing to rewrite portions of the OpenAPI document that are already covered by the Jakarta Restful Web Services framework.
[role='code_command hotspot file=0', subs="quotes"]
#Replace the InventoryResource class.#
src/main/java/io/openliberty/guides/inventory/InventoryResource.java
[role="edit_command_text"]
Add OpenAPI [hotspot=APIResponse file=0]@APIResponse, [hotspot=APIResponseSchema file=0]@APIResponseSchema, [hotspot=Operation file=0]@Operation, and [hotspot=Parameter file=0]@Parameter annotations to the two JAX-RS endpoint methods, [hotspot=host-property hotspot=Parameter file=0]getPropertiesForHost() and [hotspot=listContents file=0]listContents().
InventoryResource.java [source, Java, linenums, role='code_column hide_tags=copyright']
include::finish/src/main/java/io/openliberty/guides/inventory/InventoryResource.java[]
Clearly, there are many more OpenAPI annotations now, so let’s break them down:
[cols="35, 200", options="header"]
|===
| Annotation | Description
| [hotspot=APIResponse file=0]@APIResponse | Describes a single response from an API operation.
| [hotspot=APIResponseSchema file=0]@APIResponseSchema | Convenient short-hand way to specify a simple response with a Java class that could otherwise be specified using @APIResponse.
| [hotspot=Operation file=0]@Operation | Describes a single API operation on a path.
| [hotspot=Parameter file=0]@Parameter | Describes a single operation parameter.
|===
ifndef::cloud-hosted[] Because the Open Liberty instance was started in dev mode at the beginning of the guide, your changes were automatically picked up. Refresh the {openapi-url}[{openapi-url}^] URL to see the updated OpenAPI tree. The two endpoints at which your JAX-RS endpoint methods are served are now more meaningful: endif::[]
ifdef::cloud-hosted[] Because the Open Liberty instance was started in dev mode at the beginning of the guide, your changes were automatically picked up. Run the following curl command to see the updated OpenAPI tree:
curl http://localhost:9080/openapi
The two endpoints at which your JAX-RS endpoint methods are served are now more meaningful: endif::[]
[source, YAML, role="no_copy"]
/inventory/systems: get: summary: List inventory contents. description: Returns the currently stored host:properties pairs in the inventory. responses: "200": description: host:properties pairs stored in the inventory. content: application/json: schema: $ref: '#/components/schemas/InventoryList' /inventory/systems/{hostname}: get: summary: Get JVM system properties for particular host description: Retrieves and returns the JVM system properties from the system service running on the particular host. parameters: - name: hostname in: path description: The host for whom to retrieve the JVM system properties for. required: true schema: type: string example: foo responses: "404": description: Missing description content: application/json: {} "200": description: JVM system properties of a particular host. content: application/json: schema: type: object
OpenAPI annotations can also be added to POJOs to describe what they represent. Currently, your OpenAPI document doesn't have a very meaningful description of the [hotspot=InventoryListClass file=1]InventoryList POJO and hence it's very difficult to tell exactly what that POJO is used for. To describe the [hotspot=InventoryListClass file=1]InventoryList POJO in more detail, augment the [hotspot file=1]src/main/java/io/openliberty/guides/inventory/model/InventoryList.java file with some OpenAPI annotations.
[role='code_command hotspot file=1', subs="quotes"]
#Replace the InventoryList class.#
src/main/java/io/openliberty/guides/inventory/model/InventoryList.java
[role='edit_command_text']
Add OpenAPI [hotspot=InventoryList hotspot=Systems file=1]@Schema annotations to the [hotspot=InventoryList file=1]InventoryList class and the [hotspot=Systems file=1]systems variable.
InventoryList.java [source, Java, linenums, role='code_column hide_tags=copyright']
include::finish/src/main/java/io/openliberty/guides/inventory/model/InventoryList.java[]
Likewise, annotate the [hotspot file=2]src/main/java/io/openliberty/guides/inventory/model/SystemData.java POJO, which is referenced in the [hotspot file=1]InventoryList class.
[role='code_command hotspot file=2', subs="quotes"]
#Replace the SystemData class.#
src/main/java/io/openliberty/guides/inventory/model/SystemData.java
[role='edit_command_text']
Add OpenAPI [hotspot=SystemData hotspot=Hostname hotspot=Properties file=2]@Schema annotations to the [hotspot=SystemData file=2]SystemData class, the [hotspot=Hostname file=2]hostname variable and the [hotspot=Properties file=2]properties variable.
SystemData.java [source, Java, linenums, role='code_column hide_tags=copyright']
include::finish/src/main/java/io/openliberty/guides/inventory/model/SystemData.java[]
ifndef::cloud-hosted[] Refresh the {openapi-url}[{openapi-url}^] URL to see the updated OpenAPI tree: endif::[]
ifdef::cloud-hosted[] Run the following curl command to see the updated OpenAPI tree:
curl http://localhost:9080/openapi
endif::[]
[source, YAML, role="no_copy"]
components: schemas: InventoryList: description: POJO that represents the inventory contents. required: - systems type: object properties: systems: type: array items: $ref: '#/components/schemas/SystemData' total: format: int32 type: integer SystemData: description: POJO that represents a single inventory entry. required: - hostname - properties type: object properties: hostname: type: string properties: type: object
=== Filtering the OpenAPI tree elements
Filtering of certain elements and fields of the generated OpenAPI document can be done by using the OASFilter interface.
[role="code_command hotspot file=0", subs="quotes"]
#Create the InventoryOASFilter class.#
src/main/java/io/openliberty/guides/inventory/filter/InventoryOASFilter.java
InventoryOASFilter.java [source, Java, linenums, role='code_column hide_tags=copyright']
include::finish/src/main/java/io/openliberty/guides/inventory/filter/InventoryOASFilter.java[]
The [hotspot=filterAPIResponse file=0]filterAPIResponse() method allows filtering of APIResponse elements. When you override this method, it will be called once for every APIResponse element in the OpenAPI tree. In this case, you are matching the 404 response that is returned by the /inventory/systems/{hostname} endpoint and setting the previously missing description. To remove an APIResponse element or another filterable element, simply return null.
The [hotspot=filterOpenAPI file=0]filterOpenAPI() method allows filtering of the singleton [hotspot=OpenAPI file=0]OpenAPI element. Unlike other filter methods, when you override [hotspot=filterOpenAPI file=0]filterOpenAPI(), it is called only once as the last method for a particular filter. Hence, make sure that it doesn't override any other filter operations that are called before it. Your current OpenAPI document doesn't provide much information on the application itself or on what server and port it runs on. This information is usually provided in the info and servers elements, which are currently missing. Use the OASFactory class to manually set these and other elements of the OpenAPI tree from the org.eclipse.microprofile.openapi.models package. The [hotspot=OpenAPI file=0]OpenAPI element is the only element that cannot be removed, because that would mean removing the whole OpenAPI tree.
Each filtering method is called once for each corresponding element in the model tree. You can think of each method as a callback for various key OpenAPI elements.
Before you can use the filter class that you created, you need to create the [hotspot file=1]microprofile-config.properties file.
[role="code_command hotspot file=1", subs="quotes"]
#Create the configuration file.#
src/main/resources/META-INF/microprofile-config.properties
microprofile-config.properties [source, text, linenums, role='code_column hide_tags=Scan']
include::finish/src/main/resources/META-INF/microprofile-config.properties[]
This configuration file is picked up automatically by MicroProfile Config and registers your filter by passing in the fully qualified name of the filter class into the [hotspot=Config file=1]mp.openapi.filter property.
ifndef::cloud-hosted[] Refresh the {openapi-url}[{openapi-url}^] URL to see the updated OpenAPI tree: endif::[]
ifdef::cloud-hosted[] Run the following curl command to see the updated OpenAPI tree:
curl http://localhost:9080/openapi
endif::[]
[source, yaml, role="no_copy"]
info: title: Inventory App description: App for storing JVM system properties of various hosts. license: name: Eclipse Public License - v 2.0 url: https://www.eclipse.org/legal/epl-2.0 version: "1.0" servers:
- url: "http://localhost:{port}" description: Simple Open Liberty. variables: port: default: "9080" description: Server HTTP port.
[source, yaml, role="no_copy"]
responses: "404": description: Invalid hostname or the system service may not be running on the particular host. content: application/json: {}
For more information about which elements you can filter, see the https://openliberty.io/docs/ref/microprofile/[MicroProfile API documentation^].
To learn more about MicroProfile Config, visit the MicroProfile Config https://github.com/eclipse/microprofile-config[GitHub repository^] and try one of the MicroProfile Config https://openliberty.io/guides/?search=Config[guides^].
// ================================================================================================= // Pre-generated APIs // =================================================================================================
== Using pregenerated OpenAPI documents
As an alternative to generating the OpenAPI model tree from code, you can provide a valid pregenerated OpenAPI document to describe your APIs. This document must be named openapi with a yml, yaml, or json extension and be placed under the META-INF directory. Depending on the scenario, the document might be fully or partially complete. If the document is fully complete, then you can disable annotation scanning entirely by setting the mp.openapi.scan.disable MicroProfile Config property to true. If the document is partially complete, then you can augment it with code.
To use the pre-generated OpenAPI document, create the OpenAPI document YAML file.
[role="code_command hotspot file=0", subs="quotes"]
#Create the OpenAPI document file.#
src/main/resources/META-INF/openapi.yaml
openapi.yaml [source, text, linenums, role='code_column']
include::finish/src/main/resources/META-INF/openapi.yaml[]
This document is the same as your current OpenAPI document with extra APIs for the /inventory/properties endpoint. This document is complete so you can also add the [hotspot=Scan file=1]mp.openapi.scan.disable property and set it to true in the [hotspot file=1]src/main/resources/META-INF/microprofile-config.properties file.
[role="code_command hotspot file=1", subs="quotes"]
#Replace the configuration file.#
src/main/resources/META-INF/microprofile-config.properties
[role="edit_command_text"]
Add and set the [hotspot=Scan file=1]mp.openapi.scan.disable property to true.
microprofile-config.properties [source, text, linenums, role='code_column']
include::finish/src/main/resources/META-INF/microprofile-config.properties[]
ifndef::cloud-hosted[] Refresh the {openapi-url}[{openapi-url}^] URL to see the updated OpenAPI tree: endif::[]
ifdef::cloud-hosted[] Run the following curl command to see the updated OpenAPI tree:
curl http://localhost:9080/openapi
endif::[]
[source, yml, role="no_copy"]
/inventory/properties: get: operationId: getProperties responses: "200": description: JVM system properties of the host running this service. content: application/json: schema: type: object additionalProperties: type: string
// ================================================================================================= // Testing the services // =================================================================================================
== Testing the service
// static guide instructions: ifndef::cloud-hosted[] No automated tests are provided to verify the correctness of the generated OpenAPI document. Manually verify the document by visiting the {openapi-url}[{openapi-url}^] or the {openapi-url}/ui[{openapi-url}/ui^] URL. endif::[]
// cloud-hosted guide instructions: ifdef::cloud-hosted[] No automated tests are provided to verify the correctness of the generated OpenAPI document. Manually verify the document by visiting the http://localhost:9080/openapi or the http://localhost:9080/openapi/ui URL. endif::[]
A few tests are included for you to test the basic functionality of the inventory service. If a test failure occurs, then you might have introduced a bug into the code. These tests will run automatically as a part of the integration test suite.
[role='command'] include::{common-includes}/devmode-test.adoc[]
You will see the following output:
[source, role="no_copy"]
T E S T S
Running it.io.openliberty.guides.system.SystemEndpointIT Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.4 sec - in it.io.openliberty.guides.system.SystemEndpointIT Running it.io.openliberty.guides.inventory.InventoryEndpointIT [WARNING ] Interceptor for {http://client.inventory.guides.openliberty.io/}SystemClient has thrown exception, unwinding now Could not send Message. [err] The specified host is unknown: java.net.UnknownHostException: UnknownHostException invoking http://badhostname:9080/inventory/properties: badhostname Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.264 sec - in it.io.openliberty.guides.inventory.InventoryEndpointIT
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
The warning and error messages are expected and result from a request to a bad or an unknown hostname. This request is made in the testUnknownHost() test from the InventoryEndpointIT integration test.
[role='command'] include::{common-includes}/devmode-quit-ctrlc.adoc[]
// ================================================================================================= // Great work! You're done! // =================================================================================================
== Great work! You're done!
You have just documented and filtered the APIs of the inventory service from both the code and a static file by using MicroProfile OpenAPI in Open Liberty.
Feel free to try one of the related MicroProfile guides. They demonstrate additional technologies that you can learn and expand on top of what you built here.
For more in-depth examples of MicroProfile OpenAPI, try one of the demo applications available in the MicroProfile OpenAPI https://github.com/eclipse/microprofile-open-api/tree/master/tck/src/main/java/org/eclipse/microprofile/openapi/apps[GitHub repository^].
include::{common-includes}/attribution.adoc[subs="attributes"]