kiltek
kiltek

Reputation: 3211

Grails 3.1.4 Project with Swagger Documentation

Description

I am working with Grails 3.1.4 and I am having problems documenting my Controller classes with the Swagger Api.

grails -version gives me this output:

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

I tried to integrate Swagger into the project using these sources:
- https://grails.org/plugin/swaggydoc
- https://rahulsom.github.io/swaggydoc/

According to these sources I have to do the following things:

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:

I am running my Grails application with the bootRun task and browse to http://localhost:8080/myapproot/myDomainClassX/myFunction and see the String "MyDomainClassXController, myFunction(), did something" in my browser.

What not works is:

When I browse to http://localhost:8080/myapproot/api I get the "Page Not Found (404)" Error. Here i expected to see the magic of the Swagger annotations producing a documentation.

Question

What did I do wrong in the configuration of the Swagger plugin mentioned in the sources?

Upvotes: 3

Views: 1210

Answers (1)

Jim P
Jim P

Reputation: 571

If you are getting a 404 on /myapproot/api check your UrlMappings.groovy file.

Either leave this default mapping in there:

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

or at least allow

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

That should let you at least hit the swaggy controller with that URL.

Upvotes: 0

Related Questions