HTTP Server Response with JSON Array
Code Source: https://github.com/gogf/examples/tree/main/httpserver/response-json-array
Description
This example demonstrates how to implement a HTTP server that returns JSON array responses using GoFrame
. It showcases how to:
- Structure your API response as a JSON array
- Configure OpenAPI/Swagger documentation
- Use GoFrame's middleware for consistent response handling
- Define type-safe request and response structures
The example implements a /user
endpoint that returns a list of users in JSON array format.
Requirements
Structure
go.mod
: The Go module file.main.go
: The main application entry point.
Features
- JSON array response handling
- OpenAPI/Swagger integration
- Middleware-based response processing
- Type-safe request/response structures
- Clean API documentation
- Automatic response wrapping
Usage
-
Run the example:
go run main.go
-
The server will start at http://127.0.0.1:8199
-
Access the endpoints:
# Get user list
curl "http://127.0.0.1:8199/user"
# View API documentation
# Open in browser: http://127.0.0.1:8199/swagger
Implementation Details
The example demonstrates several key concepts:
- Using slice types for JSON array responses
- Configuring OpenAPI/Swagger documentation
- Implementing middleware for consistent response handling
- Structuring type-safe API endpoints
Key components:
- Response wrapper middleware for consistent JSON structure
- OpenAPI configuration for proper documentation
- Type definitions for request and response objects
- Swagger UI integration for API testing
Notes
- Response format includes a wrapper with standard fields
- OpenAPI documentation is available at
/api
- Swagger UI is available at
/swagger
- Response data is automatically wrapped by the middleware
- All responses are properly documented in OpenAPI format