Search code examples
restswaggerswagger-uidropwizard

Swagger integration into Dropwizard


I am fairly new to drop wizard (dropwizard.io) and just completed their tutorial. I would like to integrate Swagger (swagger.io) into this sample app. I found:

  • github.com/federecio/dropwizard-swagger-sample-app
  • github.com/federecio/dropwizard-swagger

The generated JSON seems to be quite similar, however I cannot expand the REST resources to see their respective operations. The only difference I noted was that the example code of the swagger integration uses SERVER whereas the official drop wizard example is using APPLICATION

Here an Image

Please could you tell me what is wrong in my approach?

Here is the code to my approach: https://github.com/geoHeil/dropwizardSwaggerIntegrationNotWorking

Edit:

for api - docs {
  "apiVersion": "0.0",
  "swaggerVersion": "1.2",
  "apis": [{
    "path": "/sample"
  }, {
    "path": "/hello-world",
    "description": "Operations about greetings"
  }]
}


for sample {
  "apiVersion": "0.0",
  "swaggerVersion": "1.2",
  "basePath": "http://geoHeil.local:8080",
  "resourcePath": "/sample",
  "apis": [{
    "path": "/sample",
    "operations": [{
      "method": "GET",
      "summary": "Sample endpoint",
      "notes": "",
      "type": "void",
      "nickname": "get",
      "authorizations": {},
      "parameters": []
    }]
  }, {
    "path": "/sample/hello-with-path-param/{name}",
    "operations": [{
      "method": "GET",
      "summary": "Sample endpoint with path param",
      "notes": "",
      "type": "void",
      "nickname": "getWithPathParam",
      "authorizations": {},
      "parameters": [{
        "name": "name",
        "required": true,
        "type": "string",
        "paramType": "path"
      }]
    }]
  }, {
    "path": "/sample/hello-with-query-param",
    "operations": [{
      "method": "GET",
      "summary": "Sample endpoint with query param",
      "notes": "",
      "type": "void",
      "nickname": "getWithQueryParam",
      "authorizations": {},
      "parameters": [{
        "name": "name",
        "required": false,
        "type": "string",
        "paramType": "query"
      }]
    }]
  }]
}

for hello - world {
  "apiVersion": "0.0",
  "swaggerVersion": "1.2",
  "basePath": "http://geoHeil.local:8080",
  "resourcePath": "/hello-world",
  "apis": [{
    "path": "/hello-world",
    "operations": [{
      "method": "GET",
      "summary": "Greetings endpoint",
      "notes": "",
      "type": "void",
      "nickname": "sayHello",
      "authorizations": {},
      "parameters": [{
        "name": "name",
        "required": false,
        "items": {
          "type": "string"
        },
        "paramType": "query"
      }]
    }]
  }]
}


Solution

  • I don't see anything obvious in the generated JSON that would cause the operations to not expand.

    However, the swagger-ui bundled with the dropwizard-swagger package is a bit old. I would try using the newer version of swagger-ui.

    I'm not sure if it would conflict with the bundled swagger-ui or not, but basically you need to clone https://github.com/swagger-api/swagger-ui and copy the /dist directory to your static content.

    Another option is to run the swagger-ui locally (just for testing purposes) but opening the /dist/index.html and pointing it at your /api-docs directory. However, in order for that to work properly, you'd need to enable CORS - https://github.com/swagger-api/swagger-ui#cors-support.

    EDIT:

    I didn't notice you edited the question with the JSON, and it made it easier to read. There's a problem with the GET /hello-world operation. It looks like it's supposed to accept an array of strings as a query parameter, but the parameter is missing a "type": "array" in its definition. I can't say what may be causing it without seeing the method's declaration and its annotations, but that's what you should be looking at.