REST API - Documentation
Preethi Pattabiraman
Experienced Backend Developer | Java & Cloud Solutions Expert | Building Scalable AWS Applications | Continuous Learner & Team Collaborator
There are some basic REST API features that are expected from any API, such as
Let's deep dive one by one in this series, starting with Documentation.
API Documentation is technical content that describes the API in detail with instructions how to use and integrate the API along with constraints
The success of any API depends on the level of API adoption and utilization, and how to make the users and applications understand how to use the APIs. This largely depends on the clarity of the API documentation. One needs to document the API’s
OpenAPI helps in this process. It is a JSON format that explains/provides the details about the API, the input/output schema, the actions, restrictions on the schema, and much more.
OpenAPI defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.
Based on this specification, Swagger is a tool that can provide a visual representation or a playground to understand the API in a clearer way. OpenAPI specification is also sometimes called Swagger Specification.
The basic structure of an OpenAPI document is:
? "openapi": "3.0.1",
? "info": {
? ? "title": "OpenAPI definition",
? ? "version": "v0"
? },
? "servers": [
? ? {
? ? ? "url": "https://localhost:8080",
? ? ? "description": "Generated server url"
? ? }
? ],
? "paths": {
? ? "/users": {
? ? ? "get": {
? ? ? ? "tags": [
? ? ? ? ? "user-resource"
? ? ? ? ],
? ? ? ? "operationId": "retrieveAllUsers",
? ? ? ? "responses": {
? ? ? ? ? "200": {
? ? ? ? ? ? "description": "OK",
? ? ? ? ? ? "content": {
? ? ? ? ? ? ? "*/*": {
? ? ? ? ? ? ? ? "schema": {
? ? ? ? ? ? ? ? ? "type": "array",
? ? ? ? ? ? ? ? ? "items": {
? ? ? ? ? ? ? ? ? ? "$ref": "#/components/schemas/User"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? },
? ? ? "post": {
? ? ? ? "tags": [
? ? ? ? ? "user-resource"
? ? ? ? ],
? ? ? ? "operationId": "createUser",
? ? ? ? "requestBody": {
? ? ? ? ? "content": {
? ? ? ? ? ? "application/json": {
? ? ? ? ? ? ? "schema": {
? ? ? ? ? ? ? ? "$ref": "#/components/schemas/User"
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? },
? ? ? ? ? "required": true
? ? ? ? },
? ? ? ? "responses": {
? ? ? ? ? "200": {
? ? ? ? ? ? "description": "OK",
? ? ? ? ? ? "content": {
? ? ? ? ? ? ? "*/*": {
? ? ? ? ? ? ? ? "schema": {
? ? ? ? ? ? ? ? ? "$ref": "#/components/schemas/User"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? },
? ? "/users/{id}": {
? ? ? "get": {
? ? ? ? "tags": [
? ? ? ? ? "user-resource"
? ? ? ? ],
? ? ? ? "operationId": "getOne",
? ? ? ? "parameters": [
? ? ? ? ? {
? ? ? ? ? ? "name": "id",
? ? ? ? ? ? "in": "path",
? ? ? ? ? ? "required": true,
? ? ? ? ? ? "schema": {
? ? ? ? ? ? ? "type": "integer",
? ? ? ? ? ? ? "format": "int32"
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ],
? ? ? ? "responses": {
? ? ? ? ? "200": {
? ? ? ? ? ? "description": "OK",
? ? ? ? ? ? "content": {
? ? ? ? ? ? ? "*/*": {
? ? ? ? ? ? ? ? "schema": {
? ? ? ? ? ? ? ? ? "$ref": "#/components/schemas/EntityModelUser"
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? },
? ? "delete": {
? ? ? "tags": [
? ? ? ? "user-resource"
? ? ? ],
? ? ? "operationId": "deleteUser",
? ? ? "parameters": [
? ? ? ? {
? ? ? ? ? "name": "id",
? ? ? ? ? "in": "path",
? ? ? ? ? "required": true,
? ? ? ? ? "schema": {
? ? ? ? ? ? "type": "integer",
? ? ? ? ? ? "format": "int32"
? ? ? ? ? }
? ? ? ? }
? ? ? ],
? ? ? "responses": {
? ? ? ? "200": {
? ? ? ? ? "description": "OK"
? ? ? ? }
? ? ? }
? ? }
? },
? "components": {
? ? "schemas": {
? ? ? "User": {
? ? ? ? "type": "object",
? ? ? ? "properties": {
? ? ? ? ? "name": {
? ? ? ? ? ? "maxLength": 2147483647,
? ? ? ? ? ? "minLength": 2,
? ? ? ? ? ? "type": "string"
? ? ? ? ? },
? ? ? ? ? "birthDate": {
? ? ? ? ? ? "type": "string",
? ? ? ? ? ? "format": "date"
? ? ? ? ? }
? ? ? ? }
? ? ? }
}
}
}
OpenAPI has the following merits:
领英推荐
But, OpenAPI is less useful when it comes to API designing and planning. Using the OpenAPI document, one can use tools like Swagger to display it as easily understandable content.
The Spring Boot group ID to generate the Swagger playground based on this OpenAPI document is org.springdoc. The Artifact ID is: springdoc-openapi-starter-webmvc-ui
The pom.xml looks like this:
The basic steps to generate the API documentation are:
4. Access https://localhost:8080/v3/api-docs to understand the OpenAPI specification
Please find the video to understand how it works:
Find the code on GitHub here.
Java|SpringBoot|Microservices
1 年Also in the world of Microservices, this file used as contract and shared/exposed to different teams whom are the consumers of the endpoint. Thus the development can be done parallelly on both the sides(consumer/, producer).
Information Protection TECDP Lead analyst@Cigna| Security + Certified|AWS CCP|AZ-900|CCNA|SAFe Scrum Master certified| Splunk core certified user.
1 年Wowe amazing effort Preethi. Love all your technical content. Looking forward for many more to come ????
Senior Technology Architect at Accenture.
1 年I have worked on this. And used insomnia Kong app to test the same.
Software Developer @ Delta Airlines
1 年This approach is on creating the api documentation after developing the api itself. In my recent project, I learnt about just the opposite approach. Develop the documentation first and from that automatically create the api end point aka Api First Approach. We created swagger first, once it was finalized by the business we added the swagger codegen dependency which created the endpoint.