Home > Article > Backend Development > Using Swagger in Beego and combining it with Postman for API testing
Beego is a web framework for rapid development of Go language web applications. It provides many features and tools to simplify the development process. These features and tools include support for Swagger (an API documentation generation tool) and Postman (an API testing tool), both of which allow developers to easily manage and test APIs. This article will introduce how to use Swagger in Beego and combine Postman performs API testing.
1. Install Swagger
Swagger is an open source framework for designing, building, documenting, and testing RESTful Web services. With Swagger, you can view the request and response of each API method in dynamically generated API documentation.
First, you need to install Swagger. Enter the following command:
go get -u github.com/swaggo/swag/cmd/swag
After the installation is complete, enter the following command again:
swag init
This will generate a docs folder in the root directory of your Beego application containing the generated Swagger documentation.
2. Integrate Swagger into Beego
Next, you need to integrate Swagger into Beego. To do this, you need to introduce swagger and beego/context dependencies in your main.go file.
import(
"github.com/astaxie/beego" "github.com/astaxie/beego/context" _ "your-app-doc-path/docs"
)
Now, add the following code in the init() function of your main.go file:
func main( ) {
if beego.BConfig.RunMode == beego.DEV { beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger" // 添加路由,可以自定义,这里设置为/swagger beego.InsertFilter("/*", beego.BeforeRouter, func(ctx *context.Context) { ctx.Output.Header("Access-Control-Allow-Origin", "*") ctx.Output.Header("Access-Control-Allow-Headers", "Content-Type,Token") ctx.Output.Header("Access-Control-Allow-Methods", "POST,GET") }) } // 注册Swagger路由 beego.BConfig.WebConfig.StaticDir["/docs"] = "docs" beego.BeeApp.Handlers.Get("/docs/*", func(ctx *context.Context) { ctx.Output.Header("Content-Type", "text/html;charset=utf-8") ctx.Output.Body(swaggerFiles.Index) })
}
This code will create a swagger folder in the root directory of your application to store Swagger UI files. When Beego is initialized, the Swagger UI directory will be registered as a static route. Because Swagger UI is a set of static HTML, CSS, and JavaScript files, they are accessed from the static resources directory.
You need to open Swagger UI in your browser by entering the following URL:
http://localhost:[port]/docs/index.html
Here, please Replace [port] with your server port number.
3. Write Swagger comments
After integrating Swagger, now you need to write Swagger comments for your API. It can be executed in the following ways:
First, create a file swagger.go, and then add the following code:
package controllers
/* Operation category
*/
// swagger:parameters add sub
type Operands struct {
// The first operand // in: path // required: true A int `json:"a"` // The second operand // in: path // required: true B int `json:"b"`
}
/* Return result
*/
/ / swagger:response OperResult
type OperandsResultWrapper struct {
// in:body Body struct { // 运算结果 Result int `json:"result,omitempty"` }
}
/ Add/
// swagger:route GET /add /{a}/{b} add
//
// For addition
//
// Supported predicates: GET
// Parameters:
// A: (path) - first operand (can only be an integer)
// B: (path) - second operand (can only be an integer)
// Accepted content:
// Produces:
// Return result (application/json)
// Error (application/json)
//
// swagger:parameters add
// swagger:response OperResult
func (o *OperationController) Add() {
// ...
}
Here, we define Swagger annotations to describe API requests and responses. Annotations use Swagger syntax to describe routing rules, command parameters, and result types. Specifically:
4. Test the API
Now, your Beego application can use Swagger and has the appropriate Swagger annotations. Next, you can use Postman to test your API.
In Postman, enter your API URL address and required parameters, and then select the GET verb. You should then be able to check that the API is working properly using a set of tests like the one in the swagger_generated_api_test.go file.
5. Summary
In this article, we introduced how to use Swagger in Beego and combine it with Postman for API testing. We first introduced how to install Swagger and then showed how to integrate Swagger in Beego. Next, we showed how to write Swagger annotations and use Postman for API testing. These steps are crucial for any developer who is using Beego to develop a RESTful web API, as they make it faster and easier for developers to develop and test high-quality web applications.
The above is the detailed content of Using Swagger in Beego and combining it with Postman for API testing. For more information, please follow other related articles on the PHP Chinese website!