Integrate swagger with Play Framework link /practice in progress

http://swagger.io/playing-with-swagger-using-swagger-and-swagger-ui-with-the-play-framework/

i just read it, yet to check and practice in full

build.sbt
----
"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"

application.conf :
----
api.version="1.0" swagger.api.basepath="http://localhost:9000"

routes
----
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()


swagger annotation
--------
@Api(value = "/api/todos", description = "Operations with Todos") @Security.Authenticated(Secured.class)
public class TodoController extends Controller {


@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 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));
     }
}


No comments:

Post a Comment