5

What I did is to use Grizzly/Jersey to host swagger-ui, which is static content.

Here's part of build.gradle:

compile 'org.glassfish.jersey.core:jersey-server:2.22.1'
compile 'org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.22.1'
compile 'org.glassfish.jersey.containers:jersey-container-grizzly2-servlet:2.22.1'

Here's how to configure static content with Grizzly:

httpServer = GrizzlyWebContainerFactory.create(uri);
httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("swagger-ui"), "/swagger");

swagger-ui is the folder under the project root folder.

Everything is fine when I access http://localhost/swagger/ but when I try http://localhost/swagger, it only gives a simple page without rendering, which seems all css/js files are missing: enter image description here

I'm wondering what's the best way to make url without trailing slash(/) to be the same as those with trailing slash.

Update: I've raised a ticket to swagger-ui: https://github.com/swagger-api/swagger-ui/issues/1966 but it said it's a configuration problem with Grizzly so another ticket for Grizzly: https://java.net/jira/browse/GRIZZLY-1823

No solution found now. I'm thinking to use another web server.

macemers
  • 2,194
  • 6
  • 38
  • 55

2 Answers2

0

I believe you either want map to the HTML file swagger-ui.html or to serve up the jar you could try this html server grizzly+jersey (.html from .jar archive)

UPDATE:

The issue is with the Grizzly routing. E.g. if you check your browser error log you'll see it's trying to load from http://localhost:18888/css/typography.css not http://localhost:18888/swagger/css/typography.css.

I couldn't find any info about how Grizzly does routing and it seems to be inconsistent. E.g. http://localhost/swagger loads index.html fine, but not swagger-ui.js which both are on the same path. I've used other servers like Nginx to serve static files and haven't had any issue like us.

A workaround is to map each swagger-ui folder separately or you could deploy Swagger as a single JAR by using the this as I already said in the comments. I also looked at using wildcards as discussed here, but didn't have any luck.

    httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("<basepath>/lib"),"/lib");
    httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("<basepath>/css"),"/css");
...
Community
  • 1
  • 1
Charlie
  • 2,004
  • 6
  • 20
  • 40
  • could you elaborate how to map to HTML file swagger-ui.html? – macemers Feb 02 '16 at 02:25
  • 1
    Are you also hosting a dynamically generated Swagger doc? For example we host our docs and swagger ui using Springfox which takes care of the configuring the web server to host swagger ui see http://springfox.github.io/springfox/docs/snapshot/ – Charlie Feb 02 '16 at 14:09
  • Yes, I'll use `swagger` to export my restful api. But for `swagger ui`, I just use the static content downloaded from guihub – macemers Feb 05 '16 at 07:05
  • Did you look at Springfox? I would deploy Swagger UI using that unless you have some other requirement. – Charlie Feb 05 '16 at 15:39
  • Not yet, I don't want to import `Springfox` into my project cause there's already a web container `Grizzly` for `swagger`. Everything is fine just want to make `/swagger` works as `/swagger/` – macemers Feb 16 '16 at 09:39
  • Got it. Last question - are you serving swagger-ui as a jar from this source https://github.com/webjars/swagger-ui? If not please post the link to the source code you want to host and I'll post the code to do it. – Charlie Feb 16 '16 at 13:00
  • no. I serve `swagger-ui` as a folder (folder name: swagger-ui) under root folder. The content is from https://github.com/swagger-api/swagger-ui/tree/master/dist – macemers Feb 16 '16 at 13:12
0

I can confirm that (as commented by alexey) this has since been fixed in a recent version of Grizzly.

You can either add this to your pom.xml or update the version number

<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-http-server</artifactId>
    <version>2.3.28</version>
</dependency>

And Grizzly will automatically return a 301 redirect from the url without the trailing slash, to one with the trailing slash.

Zout
  • 821
  • 10
  • 18