【開源】AspnetCore 2.0 自動API文檔生成組件,支持protobuffer

本文地址

http://www.cnblogs.com/likeli/p/8204054.htmlhtml

關於

API文檔自動生成,用於對APP端的開發幫助文檔生成,默認ProtoBuffer傳輸格式。git

本項目並非RESTful風格,是面向功能的API類型。APiDoc的做用是根據定義好的API接口和註釋來自動生成給內部開發者提供的API對接文檔。github

有任何問題,請在github提Issuesjson

開源地址

https://github.com/li-keli/AspnetCoreApiDocc#

Nuget下載

Install-Package AspnetCoreApiDocapi

關於ProtoBuffer

官方描述:網絡

Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.app

生成文檔示例

示例文檔

線上文檔更新後當即提示

線上文檔更新後當即提示

說明文檔

NO.1ide

引用項目後,在Startup.cs中的ConfigureServices方法加入以下代碼,進行服務註冊:工具

//註冊API文檔服務
    services.AddProtoMvc(op =>
    {
        op.IsOpenDoc = true; // 開放文檔訪問
        op.ApiOptions = new ApiOptions
        {
            //API文檔訪問的路由; 推薦和API地址訪問保持一致
            Host = "/core/v1",
            ApiName = "樣例API文檔",
            APiVersion = "v1.0",
            Copyright = "Copyright©2017-2018 api.com All Rights Reserved. ",
            ProtoBufVersion = ProtoBufEnum.Proto3,
            NetworkDocs = new List<NetworkDoc>
            {
                new NetworkDoc
                {
                    Title = "默認網絡文檔一",
                    Url = "https://www.baidu.com/"
                },
                new NetworkDoc
                {
                    Title = "個人博客",
                    Url = "http://www.cnblogs.com/likeli/"
                },
            }
        };
        //此處配置ES日誌服務地址
        //op.ESOptions = new ESOptions
        //{
        //    Uri = "http://192.168.0.1:9200",
        //    DefaultIndex = "test-log",
        //};
    });

NO.2

Configure方法啓用服務:

app.UseStatusCodePages()
        .UseApi();  //啓用API文檔生成

NO.3

在須要生成API文檔的控制器Controller``或方法Action上添加ApiDoc特性標記

例如:

Controller上添加:

[ApiDoc, Route("core/v1/[controller]/[action]/")]
    public class ApiController
    {
        ...
    }

Action上添加:

/// <summary>
    /// 獲取產品方法2
    /// </summary>
    /// <param name="input">輸入參數</param>
    /// <returns>輸出參數</returns>
    [ApiDoc, HttpPost]
    public ProductInput GetProduct2([FromBody] ProductInput input)
    {
        return new ProductInput {ProductName = "一體機"};
    }

在controller上添加ApiDoc特性後,能夠在該控制器下的action上再添加ApiDoc(false)來中止某個單獨方法的文檔生成

NO.4

給API的項目和全部其依賴的項目的.csproj文件中的Project節點下都加上生成XML的配置,以下:

<PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <DocumentationFile>bin\Debug\netcoreapp2.0\{項目名}.xml</DocumentationFile>
    <DocumentationFile>bin\Release\netcoreapp2.0\{項目名}.xml</DocumentationFile>
    <NoWarn>1701;1702;1705;1591</NoWarn>
  </PropertyGroup>

NO.5

經過遊覽器打開http://localhost:5000/core/v1/api.do來訪問API文檔

完整實例:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        public void ConfigureServices(IServiceCollection services)
        {
            //加載日誌記錄組件
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton<ESClientProvider>();
    
            //註冊API文檔服務
            services.AddProtoMvc(op =>
            {
                op.IsOpenDoc = true; // 開放文檔訪問
                op.ApiOptions = new ApiOptions
                {
                    //API文檔訪問的路由; 推薦和API地址訪問保持一致
                    Host = "/core/v1",
                    ApiName = "樣例API文檔",
                    APiVersion = "v1.0",
                    Copyright = "Copyright©2017-2018 api.com All Rights Reserved. ",
                    ProtoBufVersion = ProtoBufEnum.Proto3,
                    NetworkDocs = new List<NetworkDoc>
                    {
                        new NetworkDoc
                        {
                            Title = "默認網絡文檔一",
                            Url = "https://www.baidu.com/"
                        },
                        new NetworkDoc
                        {
                            Title = "個人博客",
                            Url = "http://www.cnblogs.com/likeli/"
                        },
                    }
                };
                //此處配置ES日誌服務地址
                //op.ESOptions = new ESOptions
                //{
                //    Uri = "http://192.168.0.1:9200",
                //    DefaultIndex = "test-log",
                //};
            });
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            //啓動ES日誌服務
            //loggerFactory
            //    .AddESLogger(app.ApplicationServices, "test-log", new FilterLoggerSettings
            //    {
            //        {"*", LogLevel.Trace},
            //        {"Microsoft", LogLevel.Warning},
            //        {"System", LogLevel.Warning},
            //    });
            app.UseStatusCodePages()
                .UseApi(); //啓用API文檔生成
        }
    }

其餘工具

在tool目錄下提供了批量下載proto文件的工具apiprotoasnic.go,基於go編寫,源代碼很簡單,就是解析json,這裏再也不單獨的提供說明文檔。

約定

  • 全部API的方法傳入參數必須從Body中讀取

版權

本項目採用 MIT 開源受權許可證

相關文章
相關標籤/搜索