【发布时间】:2016-02-02 21:33:49
【问题描述】:
我正在运行 Grails 3.0.11 并希望为我的 REST 端点创建 Swagger 文档。我将 SwaggyDoc-plugin 添加到我的 build.gradle 脚本中的依赖项中,方法是添加:
compile "org.grails.plugins:swaggydoc:0.26.0".
在 IntelliJ 中,我看到 Swaggydoc 依赖项已添加到我的库列表中。
通过 grails run-app 命令启动我的 Grails 应用程序并输入 http://localhost:8080/api/ 打开我的应用程序后,我收到一个 404 错误,提示该页面不存在。
我是否需要配置更多或运行一些特殊的东西来生成文档?我已经尝试在 Git 项目中开票并联系作者,但没有成功。
Update1:我添加了一个 Grails 3 插件(在 Versioneye 中找到?):
compile "org.grails.plugins:swaggydoc-grails3:0.26.0"
它确实工作了一半,默认情况下某种宠物演示是可见的,并且它在域和枚举中的约束上失败。实际上似乎不太好用。
Update2:正如 Dilip Krishnan 所指出的,我尝试使用 SpringFox,首先我将依赖项添加到我的 Gradle 构建文件中:
compile("io.springfox:springfox-swagger2:2.3.1")
compile("io.springfox:springfox-swagger-ui:2.3.1")
然后我添加了一个名为 ApiDocumentationConfiguration 的新类,代码如下:
@Configuration
@EnableSwagger2
public class ApiDocumentationConfiguration {
@Bean
public Docket documentation() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Bean
public UiConfiguration uiConfig() {
return UiConfiguration.DEFAULT;
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("My awesome API")
.description("Some description")
.version("1.0")
.contact("my-email@domain.org")
.build();
}
}
我的 Grails 资源文件包含以下代码:
beans = {
apiDocumentationConfiguration(ApiDocumentationConfiguration)
}
最后一步是启动应用程序并尝试加载显示 Swagger 前端的端点:
http://localhost:8080/swagger-ui.html
它在幕后尝试加载另一个端点(我猜是包含 JSON?),它加载了
http://localhost:8080/v2/api-docs
这确实显示了 JSON 数据,我得到了基本错误控制器、健康 mvc、指标 mvc 等的端点。但不是我自己的注释用户控制器,其注释如下:
@Api(value = "users", description = "Endpoint for user management")
class UserController {
// GET all users
@ApiOperation(value = "doStuff", nickname = "doStuff", response = User.class)
def index() {
respond User.list()
}
}
似乎我快到了,但仍然缺少一些东西,是我的注释错误还是没有扫描我的控制器?
Update3:与 SpringFox 的一位作者 (Dilip Krishnan) 联系以向 SpringFox 添加对 Grails 3+ 的支持,请参阅 ticket。它目前不起作用的原因是因为 SpringFox 查看 MVC 注释,需要编写一个适配器来从 Grails 中的控制器检索端点。
【问题讨论】:
-
您是否尝试过此处描述的自定义映射:rahulsom.github.io/swaggydoc/guide/customization.html?
-
@majkelo 我将
/myapi" (controller: "api")行添加到UrlMappings-class 中,但它仍然给出“错误404(找不到页面)”,“路径/myapi" - 感觉好像控制器没有在启动时生成/运行? -
由于 grails 3.x 是基于 spring 4.x 构建的,您可以尝试使用 springfox
-
@DilipKrishnan 谢谢,我不知道并尝试过,但我用一些额外的信息更新了我的原始问题,我还不能让它正常工作,我错过了什么?跨度>
-
@Tjeerd 要尝试的一件事是使用版本
2.3.1而不是2.1.1