Spring Grails3+;(3.0.11)不工作
我正在运行Grails3.0.11,并希望为我的REST端点创建Swagger文档。通过添加以下内容,我在build.gradle脚本中将-添加到依赖项中:Spring Grails3+;(3.0.11)不工作,spring,grails,swagger,Spring,Grails,Swagger,我正在运行Grails3.0.11,并希望为我的REST端点创建Swagger文档。通过添加以下内容,我在build.gradle脚本中将-添加到依赖项中: compile "org.grails.plugins:swaggydoc:0.26.0". 在IntelliJ中,我看到Swaggydoc依赖项添加到我的库列表中 在通过grailsrun-app命令启动Grails应用程序并通过输入打开应用程序后,我得到一个404错误,告诉我页面不存在 我需要配置更多的东西还是运行一些特殊的东西来生成
compile "org.grails.plugins:swaggydoc:0.26.0".
在IntelliJ中,我看到Swaggydoc依赖项添加到我的库列表中
在通过grailsrun-app命令启动Grails应用程序并通过输入打开应用程序后,我得到一个404错误,告诉我页面不存在
我需要配置更多的东西还是运行一些特殊的东西来生成文档?我已经尝试在Git项目中打开一个票证,并与作者联系,但没有成功
Update1:似乎有一个Grails 3-plugin(可以在Versioneye上找到),我添加了:
compile "org.grails.plugins:swaggydoc-grails3:0.26.0"
它只工作了一半,默认情况下某种Pet演示是可见的,并且它在域和枚举中的约束上失败。实际上似乎不太管用
更新2:正如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();
}
}
beans = {
apiDocumentationConfiguration(ApiDocumentationConfiguration)
}
我的Grails资源文件包含以下代码:
@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();
}
}
beans = {
apiDocumentationConfiguration(ApiDocumentationConfiguration)
}
最后一步是启动应用程序并尝试加载端点,该端点显示了招摇过市的前端:
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()
}
}
似乎我就快到了,但仍然缺少一些东西,我的注释是错误的还是它没有扫描我的控制器
更新3:联系SpringFox的一位作者(Dilip Krishnan),为SpringFox添加对Grails 3+的支持,请参阅。它目前不起作用的原因是SpringFox查看MVC注释,需要编写一个适配器来从Grails中的控制器检索端点。我已经在2.4.x项目和3.1.4中成功地使用了swaggydocs。 为了使它在3.x中工作(在3.1.4中测试),您必须添加
compile "org.grails.plugins:swaggydoc-grails3:0.26.0"
到渐变依赖项部分。这使得swaggy在您的项目中可用
然后向控制器添加注释
@Api("test methods")
class TestController {
@ApiOperation(value = "some method description")
@ApiResponses([
@ApiResponse(code = 405, message = "Bad method. Only POST is allowed"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 400, message = "Invalid request json")
])
def testGetMethod() {
render([status: "OK"] as JSON)
}
然后按如下方式标记您的方法allowedMethods
class TestController {
static allowedMethods = [testGetMethod: "GET", testPostMethod: "POST"]
请注意,这一点非常重要,否则swaggy会将您的每个方法标记为GET。Swaggy既不尊重ApiOperation注释中的httpMethod,也不尊重url映射中的http方法
最后,将控制器添加到url映射,因为swaggy检查url映射以查找url。注意这个箱子
//NOTE small camelCase
//swaggy won't see urls correctly if you start controller name with capital letter
"/api/test/"(controller: "test", action: "testGetMethod")
"/api/test/"(controller: "test", action: "testPostMethod")
您还可以在application.yml中添加一些api信息
swaggydoc:
contact: rafal@pydyniak.pl
description: sample swaggy app
你们可以在我的github上找到示例应用程序(使用虚拟方法,但重点是让swaggy工作)。
还有一些关于如何在上使用此api的文档。我只是想向您展示如何安装它(因为要让一切正常工作是相当棘手的)
希望有帮助 我遵循了相同的两个步骤: 1) 添加swagger2依赖项 2) 提供配置 问题是sprinfox不扫描grails url映射(请参阅) 要修复此问题,我添加了标准弹簧注释:
@Controller()
@RequestMapping(value="/path")
关于控制器和
@RequestMapping(value="/resource")
在方法上。之后,sprinfox开始获取我的文档。您是否尝试过此处描述的自定义映射:?@majkelo我在
UrlMappings
-类中添加了行/myapi”(controller:“api”)
,但它仍然给出了“error 404(page not found)”,“path/myapi”“-感觉好像控制器在启动时没有生成/运行?由于grails 3.x是在spring 4.x上构建的,您可以尝试使用@DilipKrishnan谢谢,我不知道这一点,并尝试了它,但我用一些附加信息更新了我的原始问题,我还不能让它正常工作,我还缺少什么?@Tjeerd要尝试的一件事是使用版本2.3.1
而不是2.1.1
,为了使招摇过市的ui正常工作,我不得不在application.ymlThanks的grails
下添加“resources:pattern:'/**`作为推荐。现在在中有了对它的一流支持。您是否有2.4.x应用程序实现的示例?@JJHolloway您还需要它吗?我没有这样的例子,但我想如果你还需要的话,我可以为你做一个:)现在一切正常,谢谢。swaggerDoc API控制器被安全插件阻止。有一次我把它“移动”到允许它正常工作