spring – Grails 3(3.0.11)中的Swagger 2.0支持不起作用
我正在运行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"
它确实工作了一半,默认情况下某种Pet-demo是可见的,它在域和枚举中的约束失败.实际上看起来效果不好.
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();
}
}
My 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中的控制器检索端点.