其他分享
首页 > 其他分享> > ABP.vNext系列之Swagger集成

ABP.vNext系列之Swagger集成

作者:互联网

​ 在前后端分离的项目上,后端开发完成的接口,如何快速方便的提供给前端调用,目前使用的较多便是Swagger。实际上ABP也集成了Swagger。

​ 首先使用 Install-Package Volo.Abp.Swashbuckle 添加nuget包,添加AbpSwashbuckleModule依赖:

[DependsOn(
    //...other dependencies
    typeof(AbpSwashbuckleModule) // <-- Add module dependency like that
    )]
public class YourModule : AbpModule
{
}

配置Swagger

public override void ConfigureServices(ServiceConfigurationContext context)
{
    var services = context.Services;

    //... other configurations.

    services.AddAbpSwaggerGen(
        options =>
        {
            options.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "v1" });
            options.DocInclusionPredicate((docName, description) => true);
            options.CustomSchemaIds(type => type.FullName);
        }
    );
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
    var app = context.GetApplicationBuilder();

    //... other configarations.

    app.UseAbpSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "Test API");
    });
    
    //... other configarations.
}

如果要隐藏 ABP 的默认端点,可以在 Swagger 配置中调用 HideAbpEndpoints 方法

services.AddAbpSwaggerGen(
    options => 
    {
        //... other options
        
        //Hides ABP Related endpoints on Swagger UI
        options.HideAbpEndpoints();
    }
)

权限配置

​ ABP集成了Identity Server来做权限认证,需要使用 AddAbpSwaggerGenWithOAuth 扩展在Module的 ConfigureServices 方法中使用 OAuth issuerscopes 来配置 Swagger。

public override void ConfigureServices(ServiceConfigurationContext context)
{
    var services = contex.Services;

    //... other configarations.

    services.AddAbpSwaggerGenWithOAuth(
        "https://localhost:44341",             // authority issuer
        new Dictionary<string, string>         //
        {                                      // scopes
            {"Test", "Test API"}               //
        },                                     //
        options =>
        {
            options.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "v1" });
            options.DocInclusionPredicate((docName, description) => true);
            options.CustomSchemaIds(type => type.FullName);
        }
    );
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
    var app = context.GetApplicationBuilder();

    //... other configarations.

    app.UseAbpSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "Test API");

        var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
        options.OAuthClientId("Test_Swagger"); // clientId
        options.OAuthClientSecret("1q2w3e*");  // clientSecret
    });
    
    //... other configarations.
}

标签:vNext,...,ABP,other,context,Test,Swagger,options
来源: https://www.cnblogs.com/jesen1315/p/16198596.html