Getting swagger-core 1.5 to work with Jersey and Grizzly

926 views Asked by At

I have a Jersey2 application that runs on an embedded Grizzly server - a setup identical to this example: https://github.com/jersey/jersey/tree/2.18/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly

I have integrated it with swagger-jersey2-jaxrs_2.10 and it has been working OK. Now that swagger-core 1.5 came out and it produces Swagger 2.0 definitions, I would like to upgrade to that version.

Having followed the Swagger setup instructions from this site: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 I discovered that Swagger won't work any more as it requires a ServletContext instance to be injected, and ServletContext fields annotated with @Context are not being injected in my project (they show as nulls).

So my actual question is: does jersey-container-grizzly2-servlet support ServletContext at all? Is there any way I can get ServletContext to be injected by altering my project's config? Or should I look into ways of integrating swagger-core 1.5 that don't require a ServletContext?

1

There are 1 answers

0
rieztra On

This is how I got it working:

Add these dependencies to your pom.xml:

    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-jersey2-jaxrs</artifactId>
      <version>1.5.0</version>
    </dependency> 

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-servlet</artifactId>
    </dependency>

Register ApiListingResource and SwaggerSerializers:

@ApplicationPath("/")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(io.swagger.jaxrs.listing.ApiListingResource.class);
        classes.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
        return classes;
    }
}

Initialize Grizzly, Jersey and Swagger:

public class Main
{
    private final static Logger logger = LogManager.getLogger(Main.class);

    public static final String BASE_URI = "http://0.0.0.0:8080";

    public static HttpServer startServer()
    {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.2");
        beanConfig.setBasePath("/");
        beanConfig.setResourcePackage("your packages");
        beanConfig.setScan(true);

        HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), new ResourceConfig());

        // Initialize and register Jersey Servlet
        WebappContext context = new WebappContext("WebappContext", "");
        ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
        registration.setInitParameter("javax.ws.rs.Application", MyApplication.class.getName());
        registration.addMapping("/*");

        context.deploy(httpServer);

        return httpServer;
    }

    public static void main(String[] args) throws Exception
    {
        startServer();
    }
}