Swagger-core: Any workaround for Custom application class setup for Swagger with a Jersey ResourceConfig

Created on 19 Jan 2016  Â·  3Comments  Â·  Source: swagger-api/swagger-core

I am following the upgrade guide for 1.3.12 to 1.5.6 along with the Jersey 2.X Project Setup guide. I'm running into a problem with the 'Custom Application' subclass, which provides the sample code below:

public class SampleApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet();

        //resources.add(FirstResource.class);
        //resources.add(SecondResource.class);
        //...

        resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
        resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);

        return resources;
    }
}

Our project deviates from the example you provide because it extends a Jersey ResourceConfig class, rather than Application class directly. The problem is that the ResourceConfig class itself overrides the getClasses() method and declares it final.

Are you aware of any way to work around this restriction, either by adding the necessary resources to some other part of the jersey application or through some other means of coaxing swagger? Adding them via the ResourceConfig.register() helper method does make the swagger.json file available at the usual path, but does not populate it with the documentation for the endpoint classes that have been annotated with @Api and are also registered() (and used to work before upgrading). I don't want to go down the route of the web.xml servlet or filter.

The generated swagger.json just contains the extra stuff I defined in the new BeanConfig, like this:

// http://localhost:8080/v1.0/swagger.json

{
  "swagger": "2.0",
  "info": {
    "description": "Description of my App.",
    "version": "1.0",
    "title": "MyApp"
  },
  "basePath": "/v1.0"
}

Thanks for your help.

Most helpful comment

host and schema are optional, yeah.
Without setResroucePackage, you'll get an empty output yeah, just as you can see ;)

All 3 comments

As long as you add the resources, it's fine. Using ResourceConfig.register() is a fine solution. You're not seeing it being populated, because you haven't completed the final step in guide - https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5#3-configure-and-initialize-swagger.

The final step you're referring to, is that the getClasses() that I mention? Or the BeanConfig bits? For the BeanConfig I have the following:

    @PostConstruct
    /**
     * Initializes Swagger Configuration
     */
    public void initializeSwaggerConfiguration() {
        final ReflectiveJaxrsScanner scanner = new ReflectiveJaxrsScanner();
        scanner.setResourcePackage("com.my.project.api");
        ScannerFactory.setScanner(scanner);
        BeanConfig config = new BeanConfig();
        config.setTitle("MyApp");
        config.setDescription("Description of my App");
        config.setVersion("1.0");
        config.setBasePath("/v1.0");
        config.setScan(true);
    }

The only thing I was missing from the example was the setResourcePackage – I'll try that tomorrow. The host and schema bits are option right? I remember reading that somewhere...

host and schema are optional, yeah.
Without setResroucePackage, you'll get an empty output yeah, just as you can see ;)

Was this page helpful?
0 / 5 - 0 ratings