如何将Swagger no config setup与Jersey 2集成 [英] How to integrate Swagger no config setup with Jersey 2

查看:117
本文介绍了如何将Swagger no config setup与Jersey 2集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Tomcat 8.5上托管的Jersey 2项目进行准系统的Swagger设置.我首先从Jersey入门指南( https ://jersey.github.io/documentation/latest/getting-started.html ):

I'm trying to do a barebones Swagger setup with a Jersey 2 project hosted on Tomcat 8.5. I first generated the jersey project with the following snippet from the Jersey getting started guide (https://jersey.github.io/documentation/latest/getting-started.html):

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---入门):

Then I added the Swagger dependencies from the swagger getting started guide (https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Getting-started):

<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 我得到了正确的答复.当我点击 http://localhost:8080/simple-service-webapp/webapi /openapi.json 我得到404未找到.

When hit the api at 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 I get a 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>

推荐答案

您仍然需要注册

You still need to register the OpenApi resources. These are the JAX-RS resource classes that serve up the JSON. Since you are using web.xml for your configuration, you can simply add the swagger package to the list of packages to scan.

<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扫描该程序包,并将发现并注册

Notice I added the io.swagger.v3.jaxrs2.integration.resources package to the list, delimited by comma. This will tell Jersey to scan that package, and it will discover and register both the AcceptHeaderOpenApiResource and the OpenApiResource. The difference between the two is that the former serves up the path /openapi and the data format is determined by the Accept header in the request, and the latter serves up the path /openapi.{type:json|yaml}, where the data format is determined by the extension (.json or .yaml) in the path.

这篇关于如何将Swagger no config setup与Jersey 2集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆