Jersey Rest API 使用 Swagger 的文档

Jersey Rest API Documentation using Swagger

我正在尝试使用 Swagger 在 API 文档中查看我的球衣休息服务方法列表。浏览了 GitHub 个站点中给出的几个 examples/sample。但是当我尝试访问上下文根 link 时,我仍然无法列出我的服务方法。找不到获取 404 服务。

pom.xml:

<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.danfoss.des</groupId>
<artifactId>SampleRestProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleRestProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jersey-jaxrs</artifactId>
        <version>1.5.0</version>
    </dependency>
    <!-- <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-jaxrs_2.9.1</artifactId>
        <version>1.2.0</version>
        <scope>compile</scope>
    </dependency> -->
</dependencies>
<build>
    <finalName>SampleRestProject</finalName>
</build>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
        <servlet-name>helloworld</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>io.swagger.jaxrs.listing,com.danfoss.des</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8081/SampleRestProject/</param-value>
        </init-param> -->
        <load-on-startup>1</load-on-startup>
    </servlet>
     <servlet>
        <servlet-name>JerseyJaxrsConfig</servlet-name>
        <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8081/SampleRestProject/rest</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloworld</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <!-- <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list> -->

</web-app>

服务javaclass:

package com.danfoss.des;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.danfoss.model.Track;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Path("/helloWorld")
@Api(value="helloWorld", description="Sample hello world swagger service")
public class RESTfulHelloWorld 
{
    @GET
    @Produces("text/html")
    @Path("/startingPage")
    @ApiOperation(value="Starting of the swagger service")
    public Response getStartingPage()
    {
        String output = "Staring method is invoked";
        return Response.status(200).entity(output).build();
    }
}

My Project structure

link 正在尝试访问以查看列表:http://localhost:8081/SampleRestProject/api-docs

谁能帮我找出到底哪里出了问题,或者我是否遗漏了什么。

JerseyJaxrsConfig class 是 swagger-jersey2-jaxrs 库的一部分,因此在部署 webapp 时不可用,因为您使用的是 swagger-jersey-jaxrs 和 Jersey 1.9 (这很好)。

简单替换

<servlet-class>io.swagger.jaxrs.config.JerseyJaxrsConfig</servlet-class>

<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>

使用正确的配置 class 然后再次尝试访问 http://localhost:8081/SampleRestProject/rest/swagger.json

此外,当您这样做时,请考虑将 Swagger 的基本路径定义为相对路径,以便您能够在不同的端口上部署 webapp。

<init-param>
    <param-name>swagger.api.basepath</param-name>
    <param-value>/SampleRestProject/rest</param-value>
</init-param>