4

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

我在这里读到,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参数引入基于过滤器的配置,就像我在 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)

我被困在这一点上。任何人有任何建议/指针?

4

2 回答 2

2

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

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

于 2012-10-18T19:08:38.887 回答
0

我将审查拉取请求,但与此同时,这是我们解决此问题的方法。

1) 部署两个不同的 webapp,一个在 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”的子文件夹下

于 2012-10-20T03:11:11.577 回答