Play Swagger UI url alias

Viewed 1120

I have Swagger UI for API documentation, I use the same approach like in official specification for accessing it I use next URL:

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

But I want to use http://localhost:9000/docs/ instead. I won't want to use WS for delegating, I would like to use single line in routes, like this:

GET /docs controllers.Assets.at(path:String="/public/lib/swagger-ui", file:String="index.html?url=/assets/swagger.json")

Or

GET /docs controllers.Assets.at(path:String="/public/lib/swagger-ui", file:String="index.html")

and http://localhost:9000/docs?url=/assets/swagger.json

What shold I change so it work?

2 Answers

How it can be solved without Play-Swagger:

  1. Create directory public\swagger in your project;
  2. Download all files from https://github.com/swagger-api/swagger-ui/tree/master/dist and copy them to public\swagger;
  3. Copy your swagger.json (it's specification, right?) to public\specification;
  4. Add to your routes file next line:

GET /docs
controllers.Assets.versioned(path="/public/specification", file: Asset = "swagger.json")

You can't make shortness in route file for the URL /docs/swagger-ui/index.html?url=/assets/swagger.json because index.html is generated by swagger-ui plugin to public directory and requires access to files nearby (like js and css files). Play swagger-ui uses javascript for fetching json based description of your routes via URL parameter for further parsing this document to swagger-ui, in your case it's /assets/swagger.json endpoint.

I tried to make the mapping swagger's index.html file, so pass json location like URL parameter directly:

GET     /swagger-ui         controllers.Assets.at(path = "/public/lib/swagger-ui", file = "index.html")

Play couldn't render this page, and CSS wasn't found. I appended dummy mapping to every file in swagger's default directory /public/lib/swagger-ui:

GET     /*file              controllers.Assets.at(path = "/public/lib/swagger-ui", file)

Even after that Play couldn't properly render index.html.

Related