Swagger Integration with JAX-RS and JAXB

I am working on REST Api using JAX-RS and JAXB. I would also like to use Swagger to create documents for api. I followed the example here https://github.com/wordnik/swagger-core/wiki/java-jax-rs . So I added com.wordnik.swagger.jaxrs.listing to initialize the parameters in web.xml, as shown below:

<init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.api.resources;com.api;com.wordnik.swagger.jaxrs.listing;</param-value> </init-param> 

When I break to access localhost: 9090 / rapi / api-docs.json url, I get the following error:

 SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: class com.wordnik.swagger.core.Documentation nor any of its super class is known to this context. at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159) at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1451) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:324) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488) Caused by: javax.xml.bind.JAXBException: class com.wordnik.swagger.core.Documentation nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:611) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:486) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:320) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:177) at com.sun.jersey.json.impl.BaseJSONMarshaller.marshallToJSON(BaseJSONMarshaller.java:103) at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.writeTo(JSONRootElementProvider.java:143) at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:157) 

I fixed this by adding com.wordnik.swagger.core to my custom JAXBContextProvider, as shown below:

 public JAXBContextProvider() throws JAXBException { this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), "com.api.model.impl.v1:com.api.resource.framework:com.wordnik.swagger.core"); } 

This caused another error:

 SEVERE: The provider class, class comapi.JAXBContextProvider, could not be instantiated. Processing will continue but the class will not be utilized javax.xml.bind.JAXBException: "com.wordnik.swagger.core.Documentation" doesnt contain ObjectFactory.class or jaxb.index 

I use jaxb.index files to maintain context in relation to classes with JAXB annotations, however there is no jaxb.index file in the swagger package that includes the Documentation class. Has anyone encountered a similar problem when integrating Swagger with JAXB and JAX-RS?

+4
source share
1 answer

It turned out that I needed to create a JAXBContext in addition to the already created JSONJAXBContext.

I added

 this.swaggerJAXBcontext = JAXBContext.newInstance(com.wordnik.swagger.core.Documentation.class); 

for the constructor and this getContext method (class type):

 if (type.equals(com.wordnik.swagger.core.Documentation.class)){ return swaggerJAXBcontext; } 
+3
source

All Articles