swagger jersey REST API不起作用(swagger jersey REST API doesn't work)

我按照步骤将Swagger添加到我在TOMCAT中已经构建的Jersey REST API项目中

添加:
swagger-annotations-1.3.10.jar swagger-core_2.10-1.3.10.jar swagger-jaxrs_2.10-1.3.10.jar swagger-jersey2-jaxrs_2.10-1.3.10.jar

在web.xml中添加

contextClass org.springframework.web.context.support.AnnotationConfigWebApplicationContext contextConfigLocation com.estartup.config.PersistenceConfig

<!-- Bootstrap the root application context as usual using ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.estartup.config.Application</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>Jersey2Config</servlet-name> <servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class> <init-param> <param-name>api.version</param-name> <param-value>1.0.0</param-value> </init-param> <init-param> <param-name>swagger.api.basepath</param-name> <param-value>http://localhost:8080/api</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>

申请类:

public class Application extends ResourceConfig { @Inject public Application(ServiceLocator serviceLocator) { register(new ServiceBinder()); register(com.estartup.feature.JacksonFeature.class); packages(true, "com.estartup", "com.wordnik.swagger.jaxrs.json","com.wordnik.swagger.jersey.listing"); } }

但是,当我导航到

http://localhost:8080/api/api-docs我收到HTTP Status 404 - page not found.

:(。在日志中我确实看到swagger正在这样加载:有什么问题?

15935 DEBUG [localhost-startStop-1] com.wordnik.swagger.jaxrs.config.WebXMLReader - set api.version to 1.0.0 15935 DEBUG [localhost-startStop-1] com.wordnik.swagger.jaxrs.config.WebXMLReader - set swagger.api.basepath to http://localhost:8080/api

I followed the step to add Swagger to my already built Jersey REST API project in TOMCAT

Added:
swagger-annotations-1.3.10.jar swagger-core_2.10-1.3.10.jar swagger-jaxrs_2.10-1.3.10.jar swagger-jersey2-jaxrs_2.10-1.3.10.jar

Added in web.xml

contextClass org.springframework.web.context.support.AnnotationConfigWebApplicationContext contextConfigLocation com.estartup.config.PersistenceConfig

<!-- Bootstrap the root application context as usual using ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.estartup.config.Application</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>Jersey2Config</servlet-name> <servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class> <init-param> <param-name>api.version</param-name> <param-value>1.0.0</param-value> </init-param> <init-param> <param-name>swagger.api.basepath</param-name> <param-value>http://localhost:8080/api</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>

Application class:

public class Application extends ResourceConfig { @Inject public Application(ServiceLocator serviceLocator) { register(new ServiceBinder()); register(com.estartup.feature.JacksonFeature.class); packages(true, "com.estartup", "com.wordnik.swagger.jaxrs.json","com.wordnik.swagger.jersey.listing"); } }

However, when I navigate to

http://localhost:8080/api/api-docs I get HTTP Status 404 - page not found.

:(. In the logs I do see that swagger is being loaded like this: What is wrong?

15935 DEBUG [localhost-startStop-1] com.wordnik.swagger.jaxrs.config.WebXMLReader - set api.version to 1.0.0 15935 DEBUG [localhost-startStop-1] com.wordnik.swagger.jaxrs.config.WebXMLReader - set swagger.api.basepath to http://localhost:8080/api

最满意答案

根据您的评论,如果您的应用程序部署在Tomcat上,那么很可能是错误的URL。

假设您的应用程序的上下文根是myapp ,那么您的API本身将部署在http://localhost:8080/myapp/api/... ,同样地,swagger文档可以在http://localhost:8080/myapp/api/api-docs 。

Based on your comment, if your application is deployed on Tomcat, than that's most likely the wrong URL.

Say the context root of your application is myapp, then your API itself will be deployed available on http://localhost:8080/myapp/api/... and the same way, the swagger docs would be available on http://localhost:8080/myapp/api/api-docs.

更多推荐