Tuesday, June 28, 2016

Rest API Docs Swagger

I had a lot of problems trying to get swagger to work, finally found this resource which sort of worked for me.
First I started getting

Error creating bean with name 'com.github.rahulsom.swaggydoc.ApiController': Injection of autowired dependencies failed;
This was fixed by adding the following to depenedencies
compile "org.grails:grails-dependencies"

The test UserController, User domain worked. But when I added a very simple class of my won it would render on the http://localhost:8080/api page but clicking it did not bring down it operations.

Okay found the reason. For Swaggy to work you need a few crucial things:
  • Your controller needs the @Api annotation

@Api(value = 'ApiFile', description = 'Anything')
class ApiFileTypeController extends RestfulController {
    static responseFormats = ['json', 'xml']
    ApiFileTypeController() {
        super(ApiFileType)
    }
}
  • Your UrlMapping needs the url for the resource

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }
        "/filetypes"(resource: 'apiFileType')

        "/"(view:"/index")
        "500"(view:'/error')
        "404"(view:'/notFound')
    }
}

No comments:

Post a Comment