Jersey 2: correctly display Swagger static content without slash binding (/)

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

Here is the build.gradle part:

 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 set up static content using Grizzly :

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

swagger-ui is the folder in the root folder of the project.

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 to be missing all css / js files: enter image description here

I am wondering what is the best way to make a url without a trailing braid (/) the same as with a trailing slash.

Update: I raised a ticket for swagger-ui: https://github.com/swagger-api/swagger-ui/issues/1966 , but he said that this is a configuration problem with Grizzly , so another ticket for Grizzly : https : //java.net/jira/browse/GRIZZLY-1823

No solution found. I am thinking of using a different web server.

+5
source share
2 answers

I can confirm that (as alexey commented) this has been fixed in the latest version of Grizzly.

You can 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 301 redirects from the URL without binding the slash to one with a trailing slash.

0
source

I believe that you need a map in the swagger-ui.html HTML file or for serving the jar, you can try this grizzly + jersey html server (.html from the .jar archive)

UPDATE:

The problem is Grizzly routing. For instance. if you check your browser error log, you will see that it is trying to download from http: // localhost: 18888 / css / typography.css not http: // localhost: 18888 / swagger / css / typography.css .

I could not find any information on how Grizzly does routing, and this seems inconsistent. For instance. http: // localhost / swagger loads index.html in order but not swagger-ui.js, which are both on the same path. I used other servers, such as Nginx, to serve static files and did not have problems like us.

The workaround is to map each swagger-ui folder separately or you can deploy Swagger as a single JAR using this , as I already said in the comments. I also looked at using wildcards as discussed here , but no luck.

  httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("<basepath>/lib"),"/lib"); httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("<basepath>/css"),"/css"); ... 
0
source

All Articles