带过滤器的Bootstrapping Jersey会导致Swagger失败

时间:2012-10-18 17:38:31

标签: jersey swagger

在我的应用程序web.xml中,我最初配置了Jersey,如下所示:

<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <!-- swagger for REST API support
             codehaus for exception mappings so, for ex, POSTing malformed JSON  yields 400 bad request not 500 -->
        <param-value>com.jonarcher.rest;com.wordnik.swagger.jaxrs;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <!-- Passed thru to Swagger -->
        <param-name>api.version</param-name>
        <param-value>2.0</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

这似乎工作得很好,我可以使用swagger-ui来浏览带注释的端点的文档。

但是,我想在我的应用程序中“嵌入”swagger UI,以便浏览文档的方法随之而来。考虑到我的Jersey servlet的<url-pattern>,我遇到了为swagger-ui提供静态内容的问题。显然我可以将其改为例如/api/*,但在我的情况下这实际上是不可取的。

我读到here可以将Jersey配置为作为过滤器而不是servlet运行,然后您可以指定要从Jersey处理中排除的URL的参数:

<filter>
    <filter-name>jerseyFilter</filter-name>
    <filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
        <param-value>.*\.html</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>jerseyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这本身也很好。但是当我把混合物带入混合物时,事情就开始变得糟透了。具体来说,将com.sun.jersey.config.property.packages param引入基于过滤器的配置,就像我在servlet配置中那样:

<filter>
    <filter-name>jerseyFilter</filter-name>
    <filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
        <param-value>.*\.html</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.jonarcher.rest;com.wordnik.swagger.jaxrs;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>jerseyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

导致过滤器无法启动,并出现以下错误消息:

  SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.wordnik.swagger.jaxrs.ApiListingResourceJSON.getAllApis(javax.servlet.ServletConfig,com.sun.jersey.api.core.ResourceConfig,javax.ws.rs.core.HttpHeaders,javax.ws.rs.core.UriInfo) at parameter at index 0
  SEVERE: Method, public javax.ws.rs.core.Response com.wordnik.swagger.jaxrs.ApiListingResourceJSON.getAllApis(javax.servlet.ServletConfig,com.sun.jersey.api.core.ResourceConfig,javax.ws.rs.core.HttpHeaders,javax.ws.rs.core.UriInfo), annotated with GET of resource, class com.wordnik.swagger.jaxrs.ApiListingResourceJSON, is not recognized as valid resource method.
  SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.wordnik.swagger.jaxrs.ApiListingResourceXML.getAllApis(javax.servlet.ServletConfig,com.sun.jersey.api.core.ResourceConfig,javax.ws.rs.core.HttpHeaders,javax.ws.rs.core.UriInfo) at parameter at index 0
  SEVERE: Method, public javax.ws.rs.core.Response com.wordnik.swagger.jaxrs.ApiListingResourceXML.getAllApis(javax.servlet.ServletConfig,com.sun.jersey.api.core.ResourceConfig,javax.ws.rs.core.HttpHeaders,javax.ws.rs.core.UriInfo), annotated with GET of resource, class com.wordnik.swagger.jaxrs.ApiListingResourceXML, is not recognized as valid resource method.
Oct 18, 2012 11:27:30 AM com.sun.jersey.spi.spring.container.servlet.SpringServlet initiate
SEVERE: Exception occurred when intialization
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765)
    at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
    at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:727)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4071)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4725)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:675)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:601)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:502)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

我在这一点上陷入困​​境。任何人都有任何建议/指示?

2 个答案:

答案 0 :(得分:2)

不幸的是,这看起来像是一个招摇的限制。正如您在日志消息中看到的那样,问题在于将ServletConfig注入到swagger方法中。 ServerConfig仅在Jersey作为servlet运行时可用。为了制作swagger支持过滤器,他们还必须修改他们的方法来注入Jersey WebConfig类(这是FilterConfig和ServletConfig的包装器)。

更新:我已在Swagger中提交了解决此问题的拉取请求:https://github.com/wordnik/swagger-core/pull/64

答案 1 :(得分:0)

我会审核拉取请求,但与此同时,我们也是如何解决这个问题的。

1)部署两个不同的webapps,一个在webapps / ROOT下使用swagger UI代码,另一个在webapps / api下使用api本身。

2)添加文件处理程序以使用默认的servlet,如:

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/ui/*</url-pattern>
    <url-pattern>/ui/css/*</url-pattern>
    <url-pattern>/ui/lib/*.js</url-pattern>
    <url-pattern>/ui/images/*</url-pattern>
  </servlet-mapping>

其中ui文件位于名为“ui”

的子文件夹下