【问题标题】:Swagger for Jersey API using embedded Grizzly使用嵌入式 Grizzly 的 Jersey API 的 Swagger
【发布时间】:2015-03-26 14:48:50
【问题描述】:

我对 JAVA 很陌生,更具体地说,我对 JAVA 中基于 REST 的服务很陌生。

我将 Grizzly 用作嵌入式 Web 服务器,提供 Jersey REST API。这一切都很好,但是当我尝试添加 Swagger 来记录 API 时,它不起作用。

这是我的 POM(使用 maven)

<?xml version="1.0" encoding="UTF-8"?>
         <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/xsd/maven-4.0.0.xsd">
         <modelVersion>4.0.0</modelVersion>

        <groupId>swagger_test</groupId>
        <artifactId>swagger_grizzly_test</artifactId>
        <version>1.0-SNAPSHOT</version>

        <!-- bring in all the jersey dependencies we need, from the same version -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.glassfish.jersey</groupId>
                    <artifactId>jersey-bom</artifactId>
                    <version>2.13</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

        <dependencies>
            <!-- the web server -->
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-grizzly2-http</artifactId>
            </dependency>
            <!-- json serializer -->
            <dependency>
                <groupId>org.glassfish.jersey.media</groupId>
                <artifactId>jersey-media-json-jackson</artifactId>
                <version>2.10.1</version>
            </dependency>
            <!-- jersey for API documentation -->
            <dependency>
                <groupId>com.wordnik</groupId>
                <artifactId>swagger-jersey-jaxrs_2.10</artifactId>
                <version>1.3.12</version>
            </dependency>
        </dependencies>
    </project>

这是我的主要功能,启动服务器。请注意,我的“浏览”资源位于“资源”包下。

public class Main
    {
    public static void main(String [ ] args)
        {
        String restUrl = "http://localhost:8080";

        // Grizzly makes you add the resources you want to expose
        final ResourceConfig rc = new ResourceConfig().packages ("resources", "com.wordnik.swagger.jersey.listing");

        HttpServer server = null;
        try
            {
            server = GrizzlyHttpServerFactory.createHttpServer(URI.create (restUrl), rc);
            server.start();
            System.out.println("Started...");
            }
        catch (Exception e)
            {
            System.out.println("Failed to start (" + e.toString () + ")");
            }

        // Wait for the user to close out of the app
        try{System.in.read();} catch (IOException e) {}

        if(server != null)
            {
            server.shutdownNow ();
            }
        }
    }

最后,这是我唯一的资源。

@Path("browse")
@Api(value = "/browse", description = "Browse tags")
public class Browse
    {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Browse for tags", notes = "Returns all tags in a flat list")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 500, message = "Something wrong in Server")})
    public String browse ()
        {
        return "Hello World";
        }
    }

如果我去http://localhost:8080/api-docs 我会得到...

{
apiVersion: "1.0.0",
swaggerVersion: "1.2"
}

请注意,没有列出任何 API。我已经学习了许多教程,但我没有(直接)使用 servlet,所以我认为这有点不同?

任何帮助都会很棒!

【问题讨论】:

  • 我们的google群里有几种解决方案。我建议先看看那里。
  • 你能发个链接吗?哪个组?我在这上面花了几个小时,并进行了大量的谷歌搜索。
  • groups.google.com/forum/#!forum/swagger-swaggersocket - 我们的所有支持信息也可以在我们的网站上找到 - swagger.io

标签: jersey swagger grizzly


【解决方案1】:

在这里下载源代码https://github.com/SingleMalt/jersey2-grizzly2-swagger-demo,匹配它,它应该可以工作。我现在开始工作了。

我最大的障碍是我要从 JAR 文件加载 grizzly 服务器。由于某种原因,Jersey 无法从包名称中找到资源(包括 swagger),我需要调用 rc.register(Browse.class);直接针对每个班级。

这迫使我从“com.wordnik.swagger.jersey.listing”包中添加以下内容以使事情正常进行。

// Required to support Swagger
rc.register(JerseyApiDeclarationProvider.class);
rc.register(JerseyResourceListingProvider.class);
rc.register(ApiListingResourceJSON.class);

【讨论】:

  • 链接已失效。
猜你喜欢
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 2013-12-28
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多