NullPointer when configuring Swagger with Application subclass (Jersey + Servlets, no Spring)

714 views
Skip to first unread message

James S.

unread,
May 27, 2015, 11:59:38 AM5/27/15
to swagger-sw...@googlegroups.com
Hello,

I've tried following the config steps outlined here: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 in order to configure Swagger with an Application subclass.
I am using version Swagger version 1.5.2-M1.

This is what my web.xml looks like:
<web-app>

<servlet>
<servlet-name>my-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.org.ServiceApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
    
<servlet-mapping>
<servlet-name>my-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

And this is the com.org.ServiceApplication class, which extends Jersey's ResourceConfig (itself an Application subclass) and contains Swagger configuration code:

public class ServiceApplication extends ResourceConfig
{
    public ServiceApplication()
   
{
       
// register packages with resources
        packages
("com.org.resources", "com.org.more.resources");
       
       
// register JSON
       
register(CustomJacksonFeature.class);
       
       
// register Swagger classes
       
register(com.wordnik.swagger.jaxrs.listing.ApiListingResource.class);
       
register(com.wordnik.swagger.jaxrs.listing.SwaggerSerializers.class);
       
       
// configure Swagger
       
BeanConfig swaggerConfig = new BeanConfig();
        swaggerConfig
.setVersion("v2");
        swaggerConfig
.setHost("localhost:8080");
        swaggerConfig
.setBasePath("/my-service");
        swaggerConfig
.setResourcePackage("com.org.resources,com.org.more.resources");
        swaggerConfig
.setScan(true);
   
}
   
}

When I try hitting the swagger.json endpoint, I get this error:

HTTP Status 500 - java.lang.NullPointerException


type Exception report

message java.lang.NullPointerException

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException
	org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:397)
	org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
	org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
	org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)

root cause

java.lang.NullPointerException
	com.wordnik.swagger.jaxrs.config.BeanConfig.configure(BeanConfig.java:163)
	com.wordnik.swagger.jaxrs.listing.ApiListingResource.scan(ApiListingResource.java:52)
	com.wordnik.swagger.jaxrs.listing.ApiListingResource.getListingJson(ApiListingResource.java:79)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


Inside ApiListingResource, this is always producing null, not sure if that helps:
swagger = (Swagger)context.getAttribute("swagger");

The main reason I am trying the Application subclass approach to configuring Swagger is because I need to be able to dynamically set the "host" information (and although Swagger's 2.0 spec does not require host to be set, for my particular use case I do need it to be explicitly specified). When I had initially tried configuring Swagger via init params in web.xml I was able to get it to work successfully (albeit with a hard coded host).

Any ideas on how to move forward?

Thanks,
James

Ron Ratovsky

unread,
May 27, 2015, 12:06:29 PM5/27/15
to swagger-sw...@googlegroups.com
Please upgrade to 1.5.2-M2 and see if that helps.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

James S.

unread,
May 27, 2015, 12:13:41 PM5/27/15
to swagger-sw...@googlegroups.com
Hi Ron, yes, upgrading to M2 does resolve the problem :)

Thanks,
James
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages