4

I'm working on REST Api using JAX-RS and JAXB. I also would like to use Swagger to generate docs for the api. I followed the example here https://github.com/wordnik/swagger-core/wiki/java-jax-rs . So I have added com.wordnik.swagger.jaxrs.listing to init parameters in web.xml as 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 tray to access the 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 it by adding com.wordnik.swagger.core to my custom JAXBContextProvider as 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 have been using jaxb.index files to keep the context aware of the classes with JAXB annotations, however there is no jaxb.index file in swagger package which inludes the Documentation class. Has anyone come across similar issue when integrating Swagger with JAXB and JAX-RS?

4

1 回答 1

3

原来,除了已经创建的 JSONJAXBContext 之外,我还需要创建 JAXBContext。

我添加了

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

构造函数和 getContext(Class type) 方法:

if (type.equals(com.wordnik.swagger.core.Documentation.class)){
        return swaggerJAXBcontext;
    }
于 2012-12-12T13:35:04.793 回答