Grails 3.1.4带有Swagger文档的项目

问题描述 投票:3回答:1

Description

我正在使用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. Add dependencies to 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. Add another repository to build.gradle:

`jcenter()`

3. Annotate my Controller in the following fashion:

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

4. In the file application.yml I added:

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

5. The aforementioned sources write about a Config.groovy which I do not have, so instead of writing:

swaggydoc {
    contact = "[email protected]"
    description = "API description"
    title = "My Swagger Doc for my awesome project"
    apiVersion = "0.2cents"
}

6. into the non-existent Config.groovy, I added the same text into the file application.yml using the yml syntax:

swaggydoc:
    contact: "[email protected]"
    description: "API Description"
    title: "My Swagger Doc for my awesome project"
    apiVersion: "0.2cents"

Result

What works is:

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

What not works is:

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

Question

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

grails swagger swagger-ui swagger-2.0
1个回答
0
投票

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

在此处保留此默认映射:

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

或者至少允许

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

这应该让你至少用那个URL击中swaggy控制器。

© www.soinside.com 2019 - 2024. All rights reserved.