我按照步骤将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
答案 0 :(得分:3)
尝试此配置:
pom.xml
:
...
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
...
web.xml
:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>ProjectName</display-name>
<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>your.package.name.YourApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SwaggerBootstrap</servlet-name>
<servlet-class>your.package.name.SwaggerApplication</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>
SwaggerApplication.java
:
public class SwaggerApplication extends HttpServlet {
private static final long serialVersionUID = -6039834823506457822L;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
BeanConfig beanConfig = new BeanConfig();
final String contextPath = config.getServletContext().getContextPath();
final StringBuilder sbBasePath = new StringBuilder();
sbBasePath.append(contextPath);
sbBasePath.append("/rs");
beanConfig.setBasePath(sbBasePath.toString());
// API Info
beanConfig.setVersion("0.1");
beanConfig.setTitle("My Swagger APP");
beanConfig.setResourcePackage("your.package.name");
beanConfig.setScan(true);
}
}
要访问您的Swagger JSON:http://<your-app>/<your-context>/rs/swagger.json
答案 1 :(得分:1)
根据您的评论,如果您的应用程序部署在Tomcat上,那么很可能是错误的URL。
假设您的应用程序的上下文根目录是myapp
,那么您的API本身将在http://localhost:8080/myapp/api/...
上部署,并且同样的方式,可以在http://localhost:8080/myapp/api/api-docs
上获得swagger文档。< / p>