zoukankan      html  css  js  c++  java
  • Swagger UI教程

    文档源地址 http://www.68idc.cn/help/makewebs/qitaasks/20160621620667.html

    Swagger-UI本身只提供在线测试功能,要集成它还需要告诉它本项目提供的各种服务和参数信息。这里就需要一些工作量了,不过好在许多第三方库已经给我们完成了这一工作。我这里用的是Swashbuckle,使用它也比较简单,直接使用Nuget添加其程序包即可:

       1、初始化包  PM> Install-Package Swashbuckle

    增加该程序包时,它本身会把自己相应的一些注册的代码添加到项目中,虽然我们可以不太关心这些操作,但有的时候还是需要修改一些相关的配置的。

    2、初始化包后App_Start会添加 ,SwaggerConfig 代码如下:

    using System.Web.Http;
    using WebActivatorEx;
    using WebApp;
    using Swashbuckle.Application;
    
    [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
    
    namespace WebApp
    {
        public class SwaggerConfig
        {
            public static void Register()
            {
                var thisAssembly = typeof(SwaggerConfig).Assembly;
    
                GlobalConfiguration.Configuration
                    .EnableSwagger(c =>
                        {
                            c.SingleApiVersion("v1", "WebApp");
    
    
                        })
                    .EnableSwaggerUi(c =>
                    {
                        GetXmlCommentsPath();
                    });
            }
            private static string GetXmlCommentsPath()
            {
                return $@"{System.AppDomain.CurrentDomain.BaseDirectory}inWebApi.XML";
            }
        }
    }

    3、集成XML注释

    api 应用 ->右键->属性->生成->输出-配置XML

    4、运行程序  地址栏请求:http://localhost:5746/swagger/       逼格很高啊


    到此第一种方法完成   

    开始改造第一种方法   删除SwaggerConfig    ,修改Startup 代码如下:

    public partial class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                ConfigureAuth(app);
                HttpConfiguration config = new HttpConfiguration();
                WebApiConfig.Register(config);
                config.EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "WebAPI");
                    c.IncludeXmlComments(GetXmlCommentsPath());
                    c.ResolveConflictingActions(x => x.First());
    
                }).EnableSwaggerUi();
    
                app.UseWebApi(config);
            }
    
            private static string GetXmlCommentsPath()
            {
                return $@"{System.AppDomain.CurrentDomain.BaseDirectory}inWebApi.XML";
            }
  • 相关阅读:
    IntelliJ Idea 快速配置
    常用资源工具集合
    IntelliJ Idea 快捷键大全
    spring boot注解梳理
    boot中的Scheduling定时器使用笔记
    OKHttp使用笔记
    EasyExcel使用笔记
    自定义注解用AOP实现接口信息打印
    Java的具体集合
    linux修改系统时间
  • 原文地址:https://www.cnblogs.com/codeface/p/6274610.html
Copyright © 2011-2022 走看看