英文:
Configure Jersey 2 web.xml for Swagger/ OpenAPI v3
问题
我正在运行 Jersey 2.30.1 API,并且我想将 Swagger 文档升级到 OpenAPI v3。
在 pom.xml 中,我的新 Swagger 依赖项如下:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.17.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.0.2</version>
</dependency>
之前(使用 swagger 1.5 和 swagger-ui 2.2.10-1),我在我的 web.xml 中配置了 Swagger:
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>2.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>/api</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
现在我想要做相同的事情,但我找不到文档,说明是否以及如何可能实现。
如何在使用 OpenAPI v3 时在我的 web.xml 中定义 Swagger 基路径和 API 版本?
英文:
I am running a Jersey 2.30.1 API and I want to upgrade the Swagger documentation to OpenAPI v3.
My new swagger dependencies in the pom.xml look like this:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.17.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.0.2</version>
</dependency>
Previously (swagger 1.5 with swagger-ui 2.2.10-1), I had Swagger configured in my web.xml:
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>2.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>/api</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
I would like to do the same now, but I can't find a documentation, if and how that would be possible.
How can I define the swagger base path and API version in my web.xml with OpenAPI v3?
答案1
得分: 1
<pom.xml>:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.52.1</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.1.10</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.1.10</version>
</dependency>
<web.xml>:
The Jersey2Config servlet is completely removed from web.xml.
The servlet configuration needs to be changed for the parameter `io.swagger.v3.jaxrs2.integration.resources`
<servlet>
<servlet-name>my servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>my.package.to.scan, io.swagger.v3.jaxrs2.integration.resources</param-value>
</init-param>
</servlet>
Additional to web.xml a new file openapi.yaml has to be created under `src/main/webapp/WEB-INF`. This is responsible for creating the openapi.yaml/ openapi.json file which will be consumed by the swagger ui.
<openapi.yaml>:
prettyPrint: true
cacheTTL: 0
readAllResources: false /** Only show methods in swagger-ui that are annotated */
openAPI:
info:
version: '1.0'
title: 'my application'
servers:
- url: '/my-application'
<swagger ui>:
...
window.onload = function() {
const window.ui = SwaggerUIBundle({
url: 'https://my-domain.com/my-application/openapi.json',
dom_id: '#swagger-ui-container',
docExpansion: 'none',
defaultModelsExpandDepth: -1,
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
apisSorter: "alpha",
tagsSorter: "alpha",
operationsSorter: "alpha",
validatorUrl: null
});
}
...
英文:
After playing around with this, my solution is basically like so.
pom.xml:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.52.1</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.1.10</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.1.10</version>
</dependency>
web.xml:
The Jersey2Config servlet is completely removed from web.xml.
The servlet configuration needs to be changed for the parameter io.swagger.v3.jaxrs2.integration.resources
<servlet>
<servlet-name>my servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>my.package.to.scan, io.swagger.v3.jaxrs2.integration.resources</param-value>
</init-param>
</servlet>
Additional to web.xml a new file openapi.yaml has to be created under src/main/webapp/WEB-INF
. This is responsible for creating the openapi.yaml/ openapi.json file which will be consumed by the swagger ui.
openapi.yaml:
prettyPrint: true
cacheTTL: 0
readAllResources: false /** Only show methods in swagger-ui that are annotated */
openAPI:
info:
version: '1.0'
title: 'my application'
servers:
- url: '/my-application'
swagger ui:
...
window.onload = function() {
const window.ui = SwaggerUIBundle({
url: 'https://my-domain.com/my-application/openapi.json',
dom_id: '#swagger-ui-container',
docExpansion: 'none',
defaultModelsExpandDepth: -1,
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
apisSorter: "alpha",
tagsSorter: "alpha",
operationsSorter: "alpha",
validatorUrl: null
});
}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论