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

2022-01-22 00:00:00 url swagger javascript html

我所做的是使用 Grizzly/Jersey 来托管 swagger-ui,这是静态内容.

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

这是 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'

以下是使用 Grizzly 配置静态内容的方法:

Here's how to configure static content with Grizzly:

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

swagger-ui是项目根文件夹下的文件夹.

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

当我访问 http://localhost/swagger/ 时一切正常,但当我尝试 http://localhost/swagger 时,它只提供了一个没有渲染的简单页面,似乎所有 css/js 文件都丢失了:

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:

我想知道使不带斜杠(/)的 url 与带有斜杠的 URL 相同的最佳方法是什么.

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

更新:我已经向 swagger-ui 提出了一张票:https://github.com/swagger-api/swagger-ui/issues/1966 但它说这是 Grizzly 的配置问题,所以 Grizzly 的另一张票:https://java.net/jira/browse/GRIZZLY-1823

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.

推荐答案

我可以确认(正如 alexey 评论的那样)这个问题已经在最近版本的 Grizzly 中得到修复.

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

您可以将其添加到您的 pom.xml 或更新版本号

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>

Grizzly 会自动将 301 重定向从不带斜杠的网址返回到带有斜杠的网址.

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

相关文章