dotdx80642 2019-04-06 14:41
浏览 407

使用API​​在HTML(Swagger UI)中显示swagger api规范(json)

I have go application(gorilla/mux) that serves a specific port. I too have a swagger API specification in the form of json file. Is there any go API that can generate a swagger UI definition from the JSON file just like spring boot. I am looking at https://github.com/go-swagger/go-swagger, but i am not able to use this in my go-lang code. It seems this API can be used only from the command line.

  • 写回答

2条回答 默认 最新

  • douchuanhan8384 2019-04-06 14:54
    关注

    If you already have your APIs defined in YAML or JSON format, you can use the static 'dist' tree to render it from a browser:

    https://github.com/swagger-api/swagger-ui/tree/master/dist

    Update index.html to point to your API doc via this tag:

    url: "https://petstore.swagger.io/v2/swagger.json",
    

    And if you want to serve this static tree from your go REST-API server, add the following handler:

    fs := http.FileServer(http.Dir("dist"))
    http.Handle("/swagger/", http.StripPrefix("/swagger/", fs))
    

    This will serve the swaggers docs off the /swagger/ route - reading the content from the dist directory.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?