【问题标题】:Grails 3.1.4 Project with Swagger Documentation带有 Swagger 文档的 Grails 3.1.4 项目
【发布时间】:2016-07-01 11:46:11
【问题描述】:

说明

我正在使用 Grails 3.1.4,但在使用 Swagger Api 记录我的 Controller 类时遇到问题。

grails -version 给了我这个输出:

| Grails Version: 3.1.4
| Groovy Version: 2.4.6
| JVM Version: 1.8.0_73

我尝试使用以下来源将 Swagger 集成到项目中:
- https://grails.org/plugin/swaggydoc
- https://rahulsom.github.io/swaggydoc/

根据这些来源,我必须做以下事情:

1。添加依赖到build.gradle:

compile 'io.swagger:swagger-core:1.5.7'
compile 'io.swagger:swagger-jaxrs:1.5.7'
compile "com.github.rahulsom:swaggydoc-commons:0.24.0"
compile "org.grails.plugins:swaggydoc:0.27.0"

2。将另一个存储库添加到 build.gradle:

`jcenter()`

3。按以下方式注释我的控制器:

@Api(value = "myValue", description = "this controller does something")
@Path("/myapproot/myDomainClassX")
MyDomainClassXController{
    @GET
    @Path("/myFunction")
    def myFunction(){
        render "MyDomainClassXController, myFunction(), did something"
    }
}

4。在文件application.yml 中我添加了:

grails:
    mime:
        disable:
            accept:
                header:
                    userAgents: []

5。上述消息来源写了我没有的Config.groovy,所以不要写:

swaggydoc {
    contact = "developer@coer.com"
    description = "API description"
    title = "My Swagger Doc for my awesome project"
    apiVersion = "0.2cents"
}

6。在不存在的 Config.groovy 中,我使用 yml 语法将相同的文本添加到文件 application.yml 中:

swaggydoc:
    contact: "developer@coder.com"
    description: "API Description"
    title: "My Swagger Doc for my awesome project"
    apiVersion: "0.2cents"

结果

有效的是:

我正在使用 bootRun 任务运行我的 Grails 应用程序并浏览到 http://localhost:8080/myapproot/myDomainClassX/myFunction 并在我的浏览器中看到字符串“MyDomainClassXController, myFunction(), did something”。

不起作用的是:

当我浏览到 http://localhost:8080/myapproot/api 时,我收到 “找不到页面 (404)”错误。在这里,我希望看到 Swagger 注释产生文档的魔力。

问题

我在来源中提到的 Swagger 插件的配置中做错了什么?

【问题讨论】:

  • 你看过springfox吗?如果不是,很想得到您的反馈。

标签: grails swagger swagger-ui swagger-2.0


【解决方案1】:

如果您在 /myapproot/api 上收到 404,请检查您的 UrlMappings.groovy 文件。

要么保留此默认映射:

"/$controller/$action?/$id?(.$format)?"{
    constraints {
        // apply constraints here
    }
}

或者至少允许

get "/api/$action?"(controller: 'api')

这应该让您至少可以使用该 URL 访问 swaggy 控制器。

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 2016-03-05
    • 2018-09-09
    • 2016-02-14
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多