Itās easy to document your Play Framework REST API with Swagger
- This post origĀiĀnally ran on http://āswagāgerā.io (7/30/2015)
I havĀing been using Play FrameĀwork as a Java-based, lightning-fast REST backĀend frameĀwork for sevĀeral projects. Later, I was was excited to ļ¬nd SwagĀger and worked to inteĀgrate it into a few projects. As I strugĀgled with it the ļ¬rst time, I thought it would be useĀful to share my expeĀriĀence and creĀate a āhow-toā artiĀcle describĀing the steps to sucĀceed quickly.
In order to simĀplify things, I have started oļ¬ with an existĀing Play FrameĀwork, Java, JPA, REST project creĀated by James Ward . Jamesā project is located on GitHub so you should pull it before you start with this how-to.
How-To Steps
- First, add the depenĀdenĀcies to your build.sbt. I was able to solve a depenĀdency probĀlem with the verĀsion of Play FrameĀwork 2.3.0 used in the samĀple project and swagger-core by referĀring to GitHub issue 55o: https://āgithubā.com/āsāwāaāgāgāeārā-āaāpāiā/āsāwāaāgāgāeārā-ācāoārāeā/āiāsāsāuāeāsā/ā550.
"com.wordnik" %% "swagger-play2" % "1.3.12" exclude("org.reflections", "reflections"), "org.reflections" % "reflections" % "0.9.8" notTransitive (), "org.webjars" % "swagger-ui" % "2.1.8-M1" - Add this to your conļ¬guĀraĀtion application.conf :
api.version="1.0" swagger.api.basepath="http://localhost:9000" - Add the api-docs routes to the routes ļ¬le:
GET / controllers.Assets.at(path="/public", file="index.html") GET /api-docs controllers.ApiHelpController.getResources POST /login controllers.SecurityController.login() POST /logout controllers.SecurityController.logout() GET /api-docs/api/todos controllers.ApiHelpController.getResource(path = "/api/todos") GET /todos controllers.TodoController.getAllTodos() POST /todos controllers.TodoController.createTodo() # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file)
- Add SwagĀger AnnoĀtaĀtions to the ToDoConĀtroller (Ā @Api ):
@Api(value = "/api/todos", description = "Operations with Todos") @Security.Authenticated(Secured.class) public class TodoController extends Controller {Then the AnnoĀtaĀtions for the GET and POST methods:
@ApiOperation(value = "get All Todos", notes = "Returns List of all Todos", response = Todo.class, httpMethod = "GET") public static Result getAllTodos() { return ok(toJson(models.Todo.findByUser(SecurityController.getUser()))); } @ApiOperation( nickname = "createTodo", value = "Create Todo", notes = "Create Todo record", httpMethod = "POST", response = Todo.class ) @ApiImplicitParams( { @ApiImplicitParam( name = "body", dataType = "Todo", required = true, paramType = "body", value = "Todo" ) } ) @ApiResponses( value = { @com.wordnik.swagger.annotations.ApiResponse(code = 400, message = "Json Processing Exception") } ) public static Result createTodo() { Form<models.Todo> form = Form.form(models.Todo.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(form.errorsAsJson()); } else { models.Todo todo = form.get(); todo.user = SecurityController.getUser(); todo.save(); return ok(toJson(todo)); } } - Start the appliĀcaĀtion and go to this URL in your browser:
http://localhost:9000/assets/lib/swagger-ui/index.html?/url=http://localhost:9000/api-docs
Source Code
As menĀtioned in the beginĀning, I started with James Wardās play-rest-security on githuband made these modĀiļ¬ĀcaĀtions on my fork. For all who are interĀested, here is the source code:
NOTE: meanĀwhile, James Ward has approved my pull request to add these changes to his project ā GitHub so you should pullĀ it
| Reference: | Itās easy to document your Play Framework REST API with Swagger from our JCG partner Brian Porter at the Poornerd blog. |





Getting the following error while resolving dependencies.
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: com.wordnik#swagger-play2_2.10;1.3.12: not found
[warn] :: org.webjars#swagger-ui;2.1.8-M1: not found