APIDisplay Class
The APIDisplay class is the main engine behind generating and hosting API documentation for your Java project. It manages the API metadata, scans controller classes to extract endpoint information, generates a structured API document, and hosts this documentation on a Javalin server.
Purpose
- Manage API Metadata: Stores information such as title, version, description, and contact/license details.
- Scan Endpoints: Automatically scans your controller classes for methods annotated with
@EndPointto collect API endpoint data. - Generate Documentation: Creates an
APIDocobject that consolidates API info and endpoints. - Host Documentation: Uses Javalin to serve the documentation UI and JSON API docs over HTTP.
This class is designed for ease of use with a fluent API pattern, making setup concise and clear.
Methods Overview of APIDisplay Class
| Method | Description |
|---|---|
APIDisplay() |
Private constructor to prevent direct instantiation; use getInstance() to create an object. |
getInstance() |
Creates and returns a new instance of APIDisplay. |
setApiInfo(ApiInfo apiInfo) |
Sets the API metadata information and returns the current instance for chaining. |
scanEndpoints(List<Class<?>> controllers) |
Scans provided controller classes for endpoints annotated with @EndPoint and stores them. |
generateDocumentation() |
Generates API documentation (APIDoc) based on scanned endpoints and API info. |
configureServer(JavalinConfig config) |
Configures the Javalin server to serve static files from /public folder in classpath. |
hostToServer(Javalin server) |
Sets up routes on the Javalin server to serve API documentation UI and JSON; throws if server is null. |
main(String[] args) |
Starts the Javalin server hosting the API documentation at port 8080. |
Detailed Example: main Method
The following example demonstrates how to configure and start a Javalin server that hosts your API documentation.
public static void main(String[] args) throws ChazaAPIException {
Javalin app = Javalin.create(APIDisplay::configureServer);
APIDisplay.getInstance()
.setApiInfo(ApiInfo.getInstance()
.setTitle("API Documentation")
.setApiVersion("1")
.setDescription("API Documentation")
.setTermsOfService("Terms of Service")
.addContact("email", "abcdf@gmail.com")
.addContact("phone", "94836")
.addLicense("title", "ChazaDoc")
.addLicense("url", "www.chazaAPI.com"))
.scanEndpoints(List.of(GoodController.class, GoodController2.class))
.generateDocumentation()
.hostToServer(app);
app.start(8080);
}