bvitaliyg
bvitaliyg

Reputation: 3245

Swagger does not working well with Play Framework 2

I working on RESTful API using Play Framework 2.2.1(Java). I using IDEA and I want to add Swagger to my project.

So, first I created `project/Build.scala' file, because project by default doesn't have it. Here its contents:

import sbt._
import Keys._

object ApplicationBuild extends Build {

  val appName = "speeder"
  val appVersion = "0.1-beta"

  val appDependencies: Seq[sbt.ModuleID] = Seq(
    "com.wordnik" %% "swagger-play2" % "1.3-SNAPSHOT",
    "com.wordnik" %% "swagger-play2-utils" % "1.3-SNAPSHOT"
  )

  val main = play.Project(appName, appVersion, appDependencies).settings(
    resolvers := Seq(
      "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository",
      "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
      "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases",
      "java-net" at "http://download.java.net/maven/2",
      "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"))
}

IDEA has recognized it and suggested to import project, what I did. I've annotate my controller like this:

@Api(value = "/accounts", description = "Operation with accounts")
public class Accounts extends BaseController {

    @ApiOperation(value = "Create user", notes = "This can only be done by the logged in user.", httpMethod = "POST")
    @ApiImplicitParams(@ApiImplicitParam(name = "body", value = "Created user object", required = true, dataType = "User", paramType = "body"))
    public static Result signup() { //... }
}

I've also added the route for API documentation:

GET         /api-docs.json        controllers.ApiHelpController.getResources

Then I ran the play dependencies from the console and restart the server. So now I opening http://localhost:9000/api-docs.json, and I see this:

{"apiVersion":"0.2","swaggerVersion":"1.2","apis":[{"path":"/accounts","description":"Operation with accounts"}]}

As you can note, there is no information about annotated method, it's only about class. And my IDEA doesn't see com.wordnik.swagger.annotations package or controllers.ApiHelpController.getResources controller. The app is still compilable.

So what I did wrong?

Upvotes: 2

Views: 2269

Answers (4)

KailuoWang
KailuoWang

Reputation: 1314

Alternatively you can use this lib https://github.com/iheartradio/play-swagger

This library takes a different approach than annotation (which force you into learning a new API), you write swagger spec directly in your routes file as comments. It automatically generates parameters definition based on routes file

Upvotes: 1

mijzcx
mijzcx

Reputation: 17

On http://localhost:9000/api-docs.json which outputs a path /accounts

You can find method informations in here http://localhost:9000/api-docs.json/accounts

Upvotes: 0

Arrested Cloud
Arrested Cloud

Reputation: 11

You might need the 1.3.9-SNAPSHOT. Also you might need to specify

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

in your application.conf

Upvotes: 0

anquegi
anquegi

Reputation: 11522

Your settings are rigth if you get that JSON, finally you need to add more things to inform like the other api methods.

Then go to this page

http://swagger.wordnik.com/

and copy your.server:9000/api-docs.json, in the green area at the beginning then go and you have the well documented api, then you can make that page in your own play app

Upvotes: 0

Related Questions