2

I'm developing a microservice with Scala and Play 2.5 and trying to document my endpoints with Swagger.

I added the Swagger UI webjar to my dependencies:

"org.webjars" % "swagger-ui" % "2.1.8-M1"

And the Swagger Play2 plugin:

"io.swagger" %% "swagger-play2" % "1.5.3"

Note: Version 1.5.3 of the Swagger Play2 plugin is not officially released yet. I built it from the master branch of the project, because it is the only version that can work with Play 2.5.8, which is the version I am using.

Then, I added the following to my routes file:

GET         /assets/*file                   controllers.Assets.at(path="/public", file)
GET         /api-docs                       controllers.ApiHelpController.getResources

By doing so, I am able to access the Swagger UI through:

http://localhost:9000/assets/lib/swagger-ui/index.html?/url=http://localhost:9000/api-docs

However, that is a huge URL. I want to simplify it to something as simple as the root (/), but there are the following challenges:

  1. How can I make Play2 dynamically pass the url parameter to the static HTML page? How can I do so in a way that the host and port are adjusted to whatever they are on the server where the service is running? I considered using controllers.Default.redirect, but I couldn't find a way to interpolate the host and port in the argument string.
  2. How can I map / to assets/lib/swagger-ui/index.html and pass the url argument?
Gabriel C
  • 1,012
  • 3
  • 12
  • 30
  • Did you try with this release? https://github.com/swagger-api/swagger-play/issues/97#issuecomment-255796386 – martinscmb Oct 28 '16 at 10:23

1 Answers1

2

Here is the best way you can make things work generically without any hardcoded url and port numbers

  1. Add a route to handle Redirect

GET /swaggerDocs controller.Application.redirectDocs

  1. Use the same logic you know, but just use relative URLs instead of absolute.
def redirectDocs = Action {
    Redirect(url = "/assets/lib/swagger-ui/index.html", queryString = Map("url" -> Seq("/api-docs")))
}
saheb
  • 589
  • 3
  • 9