Swagger-UI簡單而一目了然。它能夠純碎的基於html+javascript實現,只要稍微整合一下便能成為方便的API在線測試工具。項目的設計架構中一直提倡使用TDD(測試驅動)原則來開發,swagger-ui在這方面更是能提供很大幫助。
Swagger-UI更傾向於在線測試接口和數據,但其核心是一個javascript插件,只要稍作修改,便能按需求定制出不同格式的說明文檔,在github上更是基於它集成到各種語言環境,分支眾多。
其官方提供了一個離線版本,它的使用方法十分簡單:直接在js格式的資源文件中錄入REST API的json信息,便能容易地生成不同模塊下的API列表,每個API接口描述和參數、請求方法都能在每個json數組中定制。下面是目前項目中使用到的部分預覽圖:
在.net中使用
1.NuGet包安裝
Install-Package Swashbuckle
2.安裝之后會在App_Start文件夾中添加SwaggerConfig.cs類,該類中的Register()方法會在應用程序啟動的時候調用
3.啟用生成xml文檔,右擊項目文件屬性
4.配置SwaggerConfig.cs
public class SwaggerConfig { public static void Register() { Swashbuckle.Bootstrapper.Init(GlobalConfiguration.Configuration); // NOTE: If you want to customize the generated swagger or UI, use SwaggerSpecConfig and/or SwaggerUiConfig here ... SwaggerSpecConfig.Customize(c => { c.IncludeXmlComments(GetXmlCommentsPath()); }); } protected static string GetXmlCommentsPath() { return System.String.Format(@"{0}\bin\WebApiSwagger.XML", System.AppDomain.CurrentDomain.BaseDirectory); } }
5.瀏覽地址:http://localhost:50453/swagger/ui/index.html就可以看到了