简体   繁体   中英

Swagger and web.xml

I am trying to get swagger working with my java rest api. I am following the swagger documentation . However, I am unable to get swagger to work.

For example, according to the docs, I should get a json file from swagger - but regardless of what url I try, I never get it.

<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>

Based on the swagger documentation, if I go to http://localhost:8080/indicatorsService/swagger.json , I should get the swagger json - instead I get the following error

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]

thank you.

I have a similar configuration in an application of mine, except that I use Jersey 2 instead of RestEasy.

I checked my web.xml file, which is configured in a similar way (except for the servlet class, which is specialized for Jersey 2). Here is an excerpt:

<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>

Here are a few hints:

  • My Jersey servlet is mapped to the /rest/* pattern. The global root /* may be fine too but let's go on...
  • The base path defined in the Swagger servlet includes the same /rest URI within my application.

Moreover, the URL I must use to retrieve the JSON-structured API info is the following:

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

whose URI starts with the same /rest path. This brings me to think that you should use the following address:

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

I hope I guessed right...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM