简体   繁体   English

Jersey 2:正确渲染Swagger静态内容而不用斜杠(/)

[英]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. 我做的是使用Grizzly / Jersey来主持swagger-ui ,这是静态内容。

Here's part of build.gradle : 这是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 : 以下是使用Grizzly配置静态内容的方法:

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

swagger-ui is the folder under the project root folder. swagger-ui是项目根文件夹下的文件夹。

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: 当我访问http://localhost/swagger/时, 一切都很好,但是当我尝试http://localhost/swagger ,它只提供一个没有渲染的简单页面,似乎缺少所有css / js文件: 在此输入图像描述

I'm wondering what's the best way to make url without trailing slash(/) to be the same as those with trailing slash. 我想知道在没有尾部斜杠(/)的情况下制作url的最佳方法是什么是与尾部斜杠相同的方法。

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 更新:我已经提出了票招摇的UI: https://github.com/swagger-api/swagger-ui/issues/1966但它说,它是一个配置问题与Grizzly如此另一票GrizzlyHTTPS:/ /java.net/jira/browse/GRIZZLY-1823

No solution found now. 现在找不到解决方案。 I'm thinking to use another web server. 我正在考虑使用另一个Web服务器。

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) 我相信你要么想要映射到HTML文件swagger-ui.html,要么提供jar你可以尝试这个html服务器grizzly + jersey(.html来自.jar存档)

UPDATE: 更新:

The issue is with the Grizzly routing. 问题在于Grizzly路由。 Eg 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 . 例如,如果您检查浏览器错误日志,您将看到它正在尝试从http:// localhost:18888 / css / typography.css加载而不是http:// localhost:18888 / swagger / css / typography.css

I couldn't find any info about how Grizzly does routing and it seems to be inconsistent. 我找不到关于Grizzly如何进行路由的任何信息,而且似乎不一致。 Eg http://localhost/swagger loads index.html fine, but not swagger-ui.js which both are on the same path. 例如http:// localhost / swagger加载index.html罚款,但不是swagger-ui.js,它们都在同一条路径上。 I've used other servers like Nginx to serve static files and haven't had any issue like us. 我已经使用像Nginx这样的其他服务器来提供静态文件,并且没有像我们这样的任何问题。

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. 一种解决方法是分别映射每个招摇的用户界面的文件夹,或者你可以通过使用部署扬鞭作为一个单一的JAR 这个 ,因为我已经在评论中说。 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");
...

I can confirm that (as commented by alexey) this has since been fixed in a recent version of Grizzly. 我可以确认(由alexey评论)这已经在最近版本的Grizzly中得到修复。

You can either add this to your pom.xml or update the version number 您可以将其添加到pom.xml或更新版本号

<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. 并且Grizzly将自动从没有尾部斜杠的url返回301重定向到带有斜杠的一个重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM