swagger-api / swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
http://swagger.io
Apache License 2.0
7.37k stars 2.17k forks source link

java.lang.NullPointerException at io.swagger.jaxrs.config.BeanConfig.classes #2418

Open stemar87 opened 7 years ago

stemar87 commented 7 years ago

Hi everybody, as the title suggests I experience a NPE while using BeanConfig for generating Swagger documentation for a Jersey project I'm developing. Notice: I browsed through both open/closed issues but found no solution in my specific setting.

In particular, my setting and workflow followed are as follows (please, do not hesitate to ask for further clarification if I am missing anything):

compile 'io.swagger:swagger-jersey2-jaxrs:1.5.16'

@Singleton
@javax.ws.rs.Path("foo")
@Api(value = "foo")
@Produces({ "application/json","application/xml" })
@SwaggerDefinition(info = @Info(description = "A foo service", version = "0.0.1", title = "The Foo Service",
contact = @Contact(name = "Stefano Mariani", email = "john.doe@acme.inc", url = "https://fake.address.com/"),
license = @License(name = "Apache 2.0", url = "http://www.apache.org/licenses/LICENSE-2.0")),
consumes = {"application/json", "application/xml" },
produces = { "application/json", "application/xml" },
schemes = { SwaggerDefinition.Scheme.HTTPS })

and lives in package fake.package.path

BeanConfig beanConfig = new BeanConfig();
beanConfig.setHost(this.adaptedIp + ":" + this.endpointTcp);
beanConfig.setBasePath("/api-docs");
beanConfig.setResourcePackage("fake.package.path");
beanConfig.setScan(true);
final ResourceConfig config = new ResourceConfig(this.resourceClazz,
io.swagger.jaxrs.listing.ApiListingResource.class, io.swagger.jaxrs.listing.SwaggerSerializers.class)
.packages("org.glassfish.jersey.examples.jackson").register(JacksonFeature.class)
.register(MultiPartFeature.class);
this.server = GrizzlyHttpServerFactory.createHttpServer(baseUri, config);
this.server.start();

In the above configuration I get the mentioned exception.

FYI, I tried a few changes based on open/closed issues found on Github issue tracker for Swagger core:

ReflectiveJaxrsScanner scanner = new ReflectiveJaxrsScanner();
scanner.setResourcePackage("fake.package.path");
ScannerFactory.setScanner(scanner);

based on https://github.com/swagger-api/swagger-core/issues/927

ServletContainer sc = new ServletContainer(config);
this.server = GrizzlyWebContainerFactory.create(baseUri, sc, null, null);

based on https://stackoverflow.com/questions/33681371/swagger-documentation-with-jax-rs-jersey-2-and-grizzly

but none of them worked.

Also fyi, it is worth mentioning that besides the exception stack trace I get no log messages whatsoever indicating that Swagger is doing anything. BUT, if I add the following dependencies to my Gradle build script

compile 'io.swagger:swagger-annotations:1.5.16' compile 'io.swagger:swagger-codegen:2.2.3'

I get the following log line (without changing anything else)

INFO org.reflections.Reflections - Reflections took 92 ms to scan 1 urls, producing 65 keys and 77 values

Any idea of what I am doing wrong?

:)

vanta commented 7 years ago

How about attaching stacktrace...?

stemar87 commented 7 years ago

Sorry, did the report in a rush on the train and forgot the most important thing :/ Here it is, although I'm afraid it isn't of much help...

FYI omitted trace is simply the call to beanConfig.setScan(true); in my implementation.

Exception in thread "main" java.lang.NullPointerException
    at io.swagger.jaxrs.config.BeanConfig.classes(BeanConfig.java:304)
    at io.swagger.jaxrs.config.BeanConfig.scanAndRead(BeanConfig.java:240)
    at io.swagger.jaxrs.config.BeanConfig.setScan(BeanConfig.java:221)

PS: I edited the original post which contained an inconsistency in registered package names.