arons
arons

Reputation: 147

Swagger for Jersey API using embedded Grizzly

I'm pretty new to JAVA, and more specifically REST based services in JAVA.

I'm using Grizzly as an embedded web server, serving up a Jersey REST API. That's all working great, but when I try to add in Swagger to document the API, it doesn't work.

Here is my POM (using 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>

And here is my Main function, launching the server. Note my 'Browse' resource is under the package 'resources'.

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 ();
            }
        }
    }

Lastly, here is my one and only resource.

@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";
        }
    }

If I go to http://localhost:8080/api-docs I get...

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

Note there are no APIs listed. I've followed a number of tutorials, but I'm not using servlets (directly) so I think this is a little different?

Any help would be awesome!

Upvotes: 2

Views: 2127

Answers (1)

arons
arons

Reputation: 147

Download the source code here https://github.com/SingleMalt/jersey2-grizzly2-swagger-demo, match it, and it should work. I got it working now.

My biggest hurdle is that I'm loading the grizzly server from a JAR file. For some reason Jersey can't find the resources (including swagger), from the package names and I need to call rc.register(Browse.class); directly for each class.

This forced me to add the following from the "com.wordnik.swagger.jersey.listing" package to get things working.

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

Upvotes: 1

Related Questions