我似乎无法理解如何集成Swagger来生成API文档。
网址:####:8080/MyService/rest/users/getall
我在代码和依赖项中添加了注释。
我尝试访问:####:8080/MyService/rest/但说找不到。
//web.xml
<servlet>
<servlet-name>mycompany-users-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.users.services.mycompany,com.wordnik.swagger.jersey.listing</param-value>
</init-param> `
<servlet>
<servlet-name>JerseyJaxrsConfig</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>####:8080/MyService/rest/</param-value> //not sure What this should be?
</init-param>
<load-on-startup>2</load-on-startup>`
最佳答案
前提是您已正确复制了 https://github.com/wordnik/swagger-ui 中的文件到您的项目(目录 dist
必须复制到您的 src/main/webapp
),然后您可以访问 http://... 上的 API 文档/MyService/index.html
。不要忘记修改 index.html
以便 Swagger 知道在哪里加载 API 文档:
window.swaggerUi = new SwaggerUi({
url: "http://localhost:8080/MyService/rest/api-docs",
web.xml
中的 API 基本路径必须设置为 http://.../MyService/rest
如果 rest
是您在类 javax.ws.rs.core.Application
的实现中使用注释 @ApplicationPath
定义的应用程序路径。
这是我通常做的一个例子(我不使用 web.xml
进行配置):
@ApplicationPath( "api" )
public class MyRestApplication extends Application
{
@Override
public Set<Class<?>> getClasses( )
{
Set<Class<?>> resources = new HashSet<Class<?>>( );
resources.add( ApiListingResource.class );
resources.add( ApiDeclarationProvider.class );
resources.add( ApiListingResourceJSON.class );
resources.add( ResourceListingProvider.class );
resources.add( Ping.class ); // my own resource class
swaggerConfiguration( );
return resources;
}
private void swaggerConfiguration( )
{
SwaggerConfig swaggerConfig = new SwaggerConfig( );
ConfigFactory.setConfig( swaggerConfig );
swaggerConfig.setApiVersion( "0.0.1" );
swaggerConfig.setBasePath( "http://localhost:8080/MyService/api" );
ScannerFactory.setScanner( new DefaultJaxrsScanner( ) );
ClassReaders.setReader( new DefaultJaxrsApiReader( ) );
}
}
关于java - 如何将 Swagger 与 Maven + Java + Jersey +Tomcat 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25776968/