Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any workaround for Custom application class setup for Swagger with a Jersey ResourceConfig #1613

Closed
patrick-m-m opened this issue Jan 19, 2016 · 3 comments

Comments

@patrick-m-m
Copy link

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.

@webron
Copy link
Contributor

webron commented Jan 19, 2016

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.

@patrick-m-m
Copy link
Author

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...

@webron
Copy link
Contributor

webron commented Jan 21, 2016

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

@fehguy fehguy closed this as completed Dec 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants