Swagger和web.xml

时间:2015-11-13 16:29:09

标签: java jackson jax-rs resteasy swagger

我正试图使用​​我的java rest api进行招摇。 我正在关注swagger documentation。但是,我无法大摇大摆地工作。

例如,根据文档,我应该从swagger获得一个json文件 - 但无论我尝试什么网址,我都不会得到它。

<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>
    io.swagger.jaxrs.listing.ApiListingResource,
    io.swagger.jaxrs.listing.SwaggerSerializers,
    com.my.service.rest.handler.JsonParseExceptionHandler, 
    com.my.service.rest.handler.IllegalArgumentExceptionHandler, 
    com.my.service.rest.handler.JsonMappingExceptionHandler, 
    com.my.service.rest.handler.RuntimeExceptionHandler
    </param-value>
</context-param>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>

    <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class> 
    <init-param>
        <param-name>api.version</param-name>
        <param-value>0.1</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.api.basepath</param-name>
        <param-value>http://localhost:8080/indicatorsService</param-value>

        <!-- also not working <param-value>http://localhost:8080/docs</param-value> -->
    </init-param>
</servlet>
<servlet>
    <servlet-name>JbossServlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>JbossServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

基于swagger文档,如果我转到http://localhost:8080/indicatorsService/swagger.json,我应该得到swagger json - 而不是我得到以下错误

Error in handling REST request. ExpectionMessage: Could not find resource for relative : /swagger.json of full path: http://localhost:8080/indicatorsService/swagger.json: org.jboss.resteasy.spi.NotFoundException: Could not find resource for relative : /swagger.json of full path: http://localhost:8080/indicatorsService/swagger.json
    at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:360) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:374) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:367) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:307) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:173) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:118) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_85]
谢谢。

1 个答案:

答案 0 :(得分:1)

我在我的应用程序中有类似的配置,除了我使用Jersey 2而不是RestEasy。

我检查了我的web.xml文件,该文件以类似的方式配置(除了servlet类,专门用于Jersey 2)。这是一段摘录:

<servlet>
  <servlet-name>jax-rs-ws</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>com.jeff.MyJerseyApp</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>jax-rs-ws</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>swagger-servlet</servlet-name>
  <servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig.JerseyJaxrsConfig</servlet-class>
  <init-param>
    <param-name>api.version</param-name>
    <param-value>1.1.0</param-value>
  </init-param>
  <init-param>
    <param-name>swagger.api.basepath</param-name>
    <param-value>http://127.0.0.1:8080/myapp/rest</param-value>
  </init-param>
  <load-on-startup>3</load-on-startup>
</servlet>

以下是一些提示:

  • 我的Jersey servlet映射到/rest/*模式。全局根/*也可以没事,但让我们继续......
  • Swagger servlet中定义的基本路径在我的应用程序中包含相同的/rest URI。

此外,我必须使用的URL来检索JSON结构的API信息如下:

http://127.0.0.1:8080/myapp/rest/api-docs

其URI以相同的/rest路径开头。这让我觉得你应该使用以下地址:

http://localhost:8080/indicatorsService/api-docs

我希望我猜对了......