首页 文章

Swagger for Jersey API使用嵌入式Grizzly

提问于
浏览
2

我对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(直接)所以我认为这有点不同?

任何帮助都是极好的!

1 回答

  • 1

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

    我最大的障碍是我从JAR文件中加载了灰熊服务器 . 由于某种原因, Jersey 找不到资源(包括招摇),从包名称和我需要调用rc.register(Browse.class);直接为每个 class .

    这迫使我从“com.wordnik.swagger.jersey.listing”包中添加以下内容以使工作正常 .

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

相关问题