beego配置swagger問題集錦

question 1:

schemaValidationMessages":[{"level":"error","message":"Can't read from file /swagger.json"}]

solution:

find the index.html in the swagger directory:xxx\swagger\index.html

modify the script like this:

validatorUrl: false,
<script>
window.onload = function() {
  // Build a system
  const ui = SwaggerUIBundle({
    url: "swagger.json",
    validatorUrl: false,
    dom_id: '#swagger-ui',
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })

  window.ui = ui
}
</script>

2、question 2

can't open http://localhost:8080/swagger

the url switch to http://localhost:8080/swagger/#!

3、swagger 目錄生成需要執行啓動命令:

bee run -gendoc=true -downdoc=true

4、在route.go中配置swagger api,從而在頁面上顯示出來:

func init() {
	//運行跨域請求
	//在http請求的響應流頭部加上如下信息
	//rw.Header().Set("Access-Control-Allow-Origin", "*")
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowAllOrigins:  true,
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		AllowCredentials: true,
	}))
	//自動化文檔
	ns :=
		beego.NewNamespace("/v1",
			beego.NSNamespace("/ca",
				beego.NSInclude(
					&controllers.MainController{},
					&controllers.CaController{},
				),
			),
			)
	beego.AddNamespace(ns)
	beego.Router("/", &controllers.MainController{})
	beego.Router("/ca", &controllers.CaController{})

	beego.SetStaticPath("/swagger", "swagger")
}

5、在route.go的頂部配置註解:

// @APIVersion 1.0.0
// @Title yingzi blockchain API
// @Description blockchain has every tool to get any job done, so codename for the new blockchain APIs.
// @Contact [email protected] & [email protected]

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章