An open API service indexing awesome lists of open source software.

https://github.com/xmlking/rest-api-docs

Grails REST API with swagger style API docs
https://github.com/xmlking/rest-api-docs

Last synced: 6 months ago
JSON representation

Grails REST API with swagger style API docs

Awesome Lists containing this project

README

          

rest-api-docs
-------------
API docs for Grails 3.1.x Restful API

Challenge with REST API documentation is, it quickly get out of sync with current implementation as developers changes code.

This project demonstrate how developers can generate API docs and keep in sync with code changes using rahulsom's [swaggydoc](https://github.com/rahulsom/swaggydoc) plugin.

![API Docs](./api-doc.png)

### Setup

1. Create a project

```bash
# create a new API project
grails create-app rest-api-docs
# grails create-app rest-api-docs --profile rest-api
# when using with `rest-api` profile, add `org.grails:grails-dependencies` to build.gradle
cd rest-api-docs
# create a domain and a controller
grails create-domain-class User
grails create-restful-controller User
# add URL mapping for this API in UrlMappings.groovy
"/users"(resources: 'user')
```

2. Add swaggydoc dependency after `profile` dependency

`compile "org.grails.plugins:swaggydoc-grails3:0.26.0"`

3. Optionally annotate your REST controllers

1. Simple

```groovy
@Api(value = 'user', description = 'User Management API')
class UserController extends RestfulController {
static responseFormats = ['json', 'xml']
UserController() {
super(User)
}
}
```

2. Custom

```groovy
@Api(value = 'user', description = 'User Management API')
class UserController extends RestfulController {
static responseFormats = ['json', 'xml']
UserController() {
super(User)
}
@SwaggyList
def index(Integer max) {
super.index(max)
}
@SwaggyShow
def show() {
super.show()
}
@Transactional
@SwaggySave
def save() {
super.save()
}
@Transactional
@SwaggyUpdate
def update() {
super.update()
}
@Transactional
@SwaggyDelete
def delete() {
super.delete()
}
}
```

4. Run app

```bash
gradle bootRun
```

open http://localhost:8080/api