如何将 Swagger 无配置设置与 Jersey 2 集成
How to integrate Swagger no config setup with Jersey 2
我正在尝试使用 Tomcat 8.5 上托管的 Jersey 2 项目进行准系统 Swagger 设置。我首先使用 Jersey 入门指南 (https://jersey.github.io/documentation/latest/getting-started.html) 中的以下片段生成了 jersey 项目:
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false
-DgroupId=com.example -DartifactId=simple-service-webapp -Dpackage=com.example \
-DarchetypeVersion=2.27
然后我从 swagger 入门指南 (https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Getting-started) 添加了 Swagger 依赖项:
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.0.0</version>
</dependency>
当在 http://localhost:8080/simple-service-webapp/webapi/myresource I get the correct response. When I hit http://localhost:8080/simple-service-webapp/webapi/openapi.json 处点击 api 时,我收到 404 Not Found。
有什么想法吗?
这是我的 pom 的样子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-service-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-service-webapp</name>
<build>
<finalName>simple-service-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
您还需要注册OpenApi resources。这些是为 JSON 提供服务的 JAX-RS 资源 类。由于您使用 web.xml 进行配置,您只需将 swagger 包添加到要扫描的包列表中即可。
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.example,
io.swagger.v3.jaxrs2.integration.resources
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
注意我将 io.swagger.v3.jaxrs2.integration.resources
包添加到列表中,以逗号分隔。这将告诉 Jersey 扫描那个包,它会发现并注册 AcceptHeaderOpenApiResource
and the OpenApiResource
。两者的区别是前者服务路径/openapi
,数据格式由请求中的Accept
头决定,后者服务路径/openapi.{type:json|yaml}
,其中数据格式由路径中的扩展名(.json 或 .yaml)决定。
我正在尝试使用 Tomcat 8.5 上托管的 Jersey 2 项目进行准系统 Swagger 设置。我首先使用 Jersey 入门指南 (https://jersey.github.io/documentation/latest/getting-started.html) 中的以下片段生成了 jersey 项目:
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false
-DgroupId=com.example -DartifactId=simple-service-webapp -Dpackage=com.example \
-DarchetypeVersion=2.27
然后我从 swagger 入门指南 (https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Getting-started) 添加了 Swagger 依赖项:
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.0.0</version>
</dependency>
当在 http://localhost:8080/simple-service-webapp/webapi/myresource I get the correct response. When I hit http://localhost:8080/simple-service-webapp/webapi/openapi.json 处点击 api 时,我收到 404 Not Found。
有什么想法吗?
这是我的 pom 的样子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-service-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-service-webapp</name>
<build>
<finalName>simple-service-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
您还需要注册OpenApi resources。这些是为 JSON 提供服务的 JAX-RS 资源 类。由于您使用 web.xml 进行配置,您只需将 swagger 包添加到要扫描的包列表中即可。
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.example,
io.swagger.v3.jaxrs2.integration.resources
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
注意我将 io.swagger.v3.jaxrs2.integration.resources
包添加到列表中,以逗号分隔。这将告诉 Jersey 扫描那个包,它会发现并注册 AcceptHeaderOpenApiResource
and the OpenApiResource
。两者的区别是前者服务路径/openapi
,数据格式由请求中的Accept
头决定,后者服务路径/openapi.{type:json|yaml}
,其中数据格式由路径中的扩展名(.json 或 .yaml)决定。