Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

javascript - Jersey 2: render Swagger static content correctly without trailing slash(/)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...