Publishing a Custom Swagger Document

When you create a REST API, by default a Swagger 3.0 (OpenApi) definition is generated automatically. You can access this Swagger document by suffixing the API URL with ?swagger.json or ?swagger.yaml. See Using Swagger Documents for more information.

This example demonstrates how a custom Swagger definition is published for a REST API.

Synapse configuration

Following is a sample REST API configuration with a custom Swagger definition. See the instructions on how to build and run this example.

Note

The custom Swagger file that you use for generating the API is saved to the Micro Integrator's registry. The publishSwagger element in the REST API configuration specifies the registry path. In this example, we are storing the Swagger definition in the governance registry as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<api context="/v2" name="SwaggerPetstore" publishSwagger="/_system/governance/swagger_files/simple_petstore.yaml" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST" url-mapping="/pet">
        <inSequence>
            <!--This is generated API skeleton.-->
            <!--Business Logic Goes Here-->
            <payloadFactory media-type="json">
                <format>{"Response" : "Sample Response"}</format>
                <args/>
            </payloadFactory>
            <loopback/>
        </inSequence>
        <outSequence>
            <respond/>
        </outSequence>
        <faultSequence/>
    </resource>
    <resource methods="GET" url-mapping="/pet/findByStatus">
        <inSequence>
            <!--This is generated API skeleton.-->
            <!--Business Logic Goes Here-->
            <payloadFactory media-type="json">
                <format>{"Response" : "Sample Response"}</format>
                <args/>
            </payloadFactory>
            <loopback/>
        </inSequence>
        <outSequence>
            <respond/>
        </outSequence>
        <faultSequence/>
    </resource>
</api>

Build and run

Create the artifacts:

  1. Set up WSO2 Integration Studio.
  2. Create an integration project with the modules listed below:
    • Config project
    • Registry project
    • Composite Application project.
  3. To create the REST API with the above configurations:

  4. Deploy the artifacts in your Micro Integrator.

Copy the following URLs to your browser to see the Swagger documents of your API:

  • http://localhost:8290/SwaggerPetstore?swagger.json
  • http://localhost:8290/SwaggerPetstore?swagger.yaml
Top