Web API Template 2
Version: 2.03Build REST API Server Using B4X Template
This is a very minimum template where you don't even need to connect to a database to work.
Optionally you can start with keyValueStore to persist the model. This will be a foundation to more complex Web API projects.
Depends on following libraries:
- ByteConverter
- jServer
- Json
- JavaObject
- KeyValueStore
For older version, see webapi-b4j (v1.16) https://www.b4x.com/android/forum/threads/web-api-template.133764/
GitHub: https://github.com/pyhoon/webapi-2-b4j
Tutorials:
- Getting Started
- Download File Example
- Vue 3 CRUD Example
- Using Web API Controller
- Using MinimaList Controller
- Using #Plural tag
Features
- Redesign Architecture
- The core handler - ApiHandler, acts like BaseController or Routes class
- ApiHandler routes the RequestURI to controllers e.g /web/api/v2/data
- HelpHandler (optional) generates API documentation for easy debugging without external tools or clients which embed tokens in request header. HelpHandler is now scanning through controllers class for APIs to list in the documentation instead of reading handlers from b4j project main module in version 1.x
- Web and API paths can be changed in config.ini
- Versioning can be enabled or disabled
- Simple JSON response (Map or List)
- Session can be toggled
- Cookies can be toggled
- Welcome message can be toggled
- One stop ConfigServer sub to control all the settings
- Default endpoint name is based on controller's name (e.g /web/api/v2/item for ItemController)
- Overide endpoint name using #Plural (e.g /web/api/v2/items)
- Custom version name using #Version (e.g v2, live, demo, dev, staging)
- Description is set using #Desc (i.e no more using #Desc1, #Desc2 or Literals that was very confusing in version 1.x)
- API endpoint can be hidden using #Hide
- INTRODUCING: MinimaList -> store as Map/List. API server can run without database (or optionally persist as KeyValueStore).
Code Example
B4X:
Private Sub GetOneItem (id As Long)
' #Version = v2
' #Desc = Read one Item in MinimaList
' #Elements = [":id"]
Dim M1 As Map = Main.MinimaItem.Find(id)
HRM.ResponseCode = 200
HRM.ResponseData.Initialize
HRM.ResponseData.Add(M1)
Utility.ReturnHttpResponse(HRM, Response)
End Sub
Special thanks to donors of this project:
Attachments
Last edited: