生成REST API的Swagger UI文档

我在Java中使用JAX-RS / Jersey开发了我的REST API。 我想为它转换为/生成基于Swagger的UI文档。 任何人都可以告诉我精确/步骤简单的方法如何这样做? 对不起,但他们网站上给出的步骤对我来说有点模糊。

有几种方法可以将swagger-core与您的应用程序集成,但根据您的描述,我只需按照https://github.com/swagger-api/swagger-core/wiki/Swagger所述的Wiki页面进行操作。 -Core-Jersey-1.X-Project-Setup-1.5或https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5取决于你使用的泽西版。

这些页面还链接到一组样本,您可以使用这些样本进行参考,并了解它们的工作原理。 他们还将swagger-ui直接插入其中,这样您就可以看到一整套互动。

我知道最简单的方法是使用JAXRS Analyzer maven插件。 哪个可以在GitHub上找到

  com.sebastian-daschner jaxrs-analyzer-maven-plugin 0.4    analyze-jaxrs    plaintext  example.com    

这为mvn clean install创建了swagger json。 据我所知,它不需要对web.xml等进行任何操作,因为它通过字节码分析来完成。

来源:Adam Bien博客文章和他在其中一个airhacks会议上的演示

奖励:插件的创建者解释使用情况的9分钟video

Swagger在github上有一步一步的实施文档。

您应该在方法,资源,模型上使用swagger注释。 然后,您应该按照此处所述配置您的web.xml 。 完成所有这些步骤后,您可以访问swagger-ui yourdomain / api-docs或在web.xml ApiDeclarationServlet的侦听路径中配置的其他路径。

有一个样本swagger应用程序Jax-rs / Jersey

Swagger UI是HTML,Javascript和CSS资产的无依赖关系集合,可以从符合Swagger的API动态生成漂亮的文档和沙箱。 由于Swagger UI没有依赖关系,因此您可以在任何服务器环境或本地计算机上托管它。

  • 关于获得静态依赖性的讨论很好。 通常你需要复制和粘贴swagger-ui静态。 https://github.com/swagger-api/swagger-ui/issues/758
  • Swagger UI发布https://github.com/swagger-api/swagger-ui/tree/master/dist
  • 另一个使用swagger的示例应用程序: https : //github.com/apache/camel/blob/master/examples/camel-example-servlet-rest-tomcat/src/main/webapp/WEB-INF/web.xml
  • 关于使用springboot的swagger实现的简单参考(在这种情况下不需要web.xml)。

你应该使用roaster :你可以修改源代码来添加新的注释。 下面是一个示例,说明如何在您的案例中使用它:

 package ma.cars.iscraper; import org.jboss.forge.roaster.Roaster; import org.jboss.forge.roaster.model.source.*; import java.util.List; public class Main { public static void main(String[] args) { String originalClassSourceCode = "@Path(\"user\")\n public class SomeClass { @GET\n" + " @Path(\"{userId}\")\n public Response getUserById() {\n return null; \n}"; System.out.println("Before : \n" + originalClassSourceCode); JavaClassSource javaClass = Roaster.parse(JavaClassSource.class,originalClassSourceCode ); String pathValue = null; // extract Path annotation value List> listAnnotations = javaClass.getAnnotations(); for (AnnotationSource annotation :listAnnotations) { if (annotation.getName().equals("Path")) { pathValue = annotation.getStringValue(); } } AnnotationSource apiAnnotation = javaClass.addAnnotation("com.wordnik.swagger.annotations.Api"); apiAnnotation.setLiteralValue("\"" + pathValue + "\"") ; List> methods = javaClass.getMethods(); for (MethodSource method: methods) { for (AnnotationSource annotation: method.getAnnotations()) { if (annotation.getName().equals("DELETE") || annotation.getName().equals("GET") || annotation.getName().equals("POST") || annotation.getName().equals("PUT")) { String returnTypeClass = method.getReturnType().getQualifiedName(); AnnotationSource apiOperation = method.addAnnotation("com.wordnik.swagger.annotations.ApiOperation"); apiOperation.setLiteralValue("value", "\"value\""); apiOperation.setLiteralValue("response", "\"" + returnTypeClass + ".class\""); } } } System.out.println(javaClass); } } 

这是输出:

 Before : @Path("user") public class SomeClass { @GET @Path("{userId}") public Response getUserById() { return null; } After : import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.ApiOperation;@Path("user") @Api("user") public class SomeClass { @GET @Path("{userId}") @ApiOperation(value = "value", response = "Response.class") public Response getUserById() { return null; }