Swagger整合到Dropwizard中

时间:2015-01-18 10:59:26

标签: api rest swagger dropwizard swagger-ui

我很擅长放弃向导(dropwizard.io)&刚刚完成了他们的教程我想将Swagger(swagger.io)集成到这个示例应用程序中。 我找到了:

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

生成的JSON似乎非常相似,但我无法扩展REST资源以查看其各自的操作。 我注意到的唯一区别是swagger集成的示例代码使用SERVER,而官方drop向导示例使用APPLICATION

这是一张图片(i.stack.imgur.com/QzhPa.png)

请告诉我我的方法有什么问题。 非常感谢你。以下是我的方法的代码:https://github.com/geoHeil/dropwizardSwaggerIntegrationNotWorking

编辑:

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"
      }]
    }]
  }]
}

2 个答案:

答案 0 :(得分:3)

现在有一个Swagger规范2.0示例:

https://github.com/swagger-api/swagger-samples/tree/master/java/java-dropwizard

这使用最新的swagger核心库,具有最小的依赖性。请注意,杰克逊的版本需要从dropwizard的2.3.2更新为swagger 2.4.2。

答案 1 :(得分:1)

我在生成的JSON中看不到任何会导致操作不扩展的明显内容。

然而,与dropwizard-swagger包捆绑在一起的swagger-ui有点老了。我会尝试使用较新版本的swagger-ui。

我不确定它是否会与捆绑的swagger-ui冲突,但基本上你需要克隆https://github.com/swagger-api/swagger-ui并将/ dist目录复制到你的静态内容。

另一种选择是在本地运行swagger-ui(仅用于测试目的),但打开/dist/index.html并将其指向/ api-docs目录。但是,为了使其正常工作,您需要启用CORS - https://github.com/swagger-api/swagger-ui#cors-support

编辑:

我没有注意到你用JSON编辑了这个问题,这让它更容易阅读。 GET / hello-world操作存在问题。看起来它应该接受一个字符串数组作为查询参数,但该参数在其定义中缺少"type": "array"。如果没有看到方法的声明及其注释,我无法说出可能导致它的原因,但这就是你应该看到的。