What OpenAPI and Swagger UI are
35 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A frontend developer asks on WhatsApp what POST /api/v1/products returns, and you answer from memory. A Word file goes out of date the first time a status code changes. The team needs one description that stays true.
OpenAPI: a JSON or YAML contract that describes every endpoint
An OpenAPI document (spec) is a JSON or YAML file that describes every endpoint of a REST API in a fixed structure: its paths, parameters, request bodies, responses and errors. Programs can read it, not only people.
Here is the smallest useful spec, first in JSON and then in YAML. Both hold exactly the same data.
Example 1 · A tiny OpenAPI document in JSON, and the same in YAML
{ "openapi": "3.1.0", "info": { "title": "ShopLite API", "version": "v1" }, "paths": { "/api/v1/categories/{id}": { "get": { "summary": "Get one category", "parameters": [ { "name": "id", "in": "path", "required": true, "schema": { "type": "integer", "format": "int64" } } ], "responses": { "200": { "description": "The category" }, "404": { "description": "No category has this id" } } } } }}openapi: 3.1.0
info:
title: ShopLite API
version: v1
paths:
/api/v1/categories/{id}:
get:
summary: Get one category
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64
responses:
'200':
description: The category
'404':
description: No category has this idWhat this code does
openapinames the spec version,infogives the API's title and version, andpathsholds the endpoints.- The YAML version has the same keys and values; nesting is shown by two-space indentation, and a list item starts with
-. - Status codes are object keys, so they are strings:
"200"in JSON and'200'in YAML.
The first line, openapi: 3.1.0, says which version the file follows; the current ones are 3.0 and 3.1. A Word file or README goes out of date the first time someone changes a status code, and no tool can check code against it or load it into Postman. ShopLite's API-DESIGN.md is a good contract for people, but not for tools.
YAML is the same data written with indentation instead of braces and quotes, so people find it easier to write by hand. Tools usually serve JSON; people usually edit YAML.
Reading a small spec: paths, operations, schemas, responses
An operation is one HTTP method on one path in a spec, such as get on /api/v1/categories/{id}, with its summary, parameters, request body and responses.
This spec describes one endpoint with two responses. Each response points to a schema, a shape defined once under components.
Example 2 · categories.yaml: one endpoint, two responses, two schemas
Runs in: the shoplite-api Spring Boot project
openapi: 3.1.0info: title: ShopLite API version: v1servers: - url: http://localhost:8080paths: /api/v1/categories/{id}: get: tags: - Categories summary: Get one category operationId: getCategory parameters: - name: id in: path required: true description: Category id schema: type: integer format: int64 example: 2 responses: "200": description: The category content: application/json: schema: $ref: "#/components/schemas/Category" "404": description: No category has this id content: application/problem+json: schema: $ref: "#/components/schemas/Problem"components: schemas: Category: type: object required: [id, name, parentId] properties: id: type: integer format: int64 example: 2 name: type: string example: Mobiles parentId: type: [integer, "null"] format: int64 description: null for a top-level category example: 1 Problem: type: object properties: type: type: string example: https://shoplite.example/problems/not-found title: type: string example: Resource not found status: type: integer example: 404 detail: type: string example: Category 99 was not found. instance: type: string example: /api/v1/categories/99$ curl -i http://localhost:8080/api/v1/categories/2
HTTP/1.1 200
Content-Type: application/json
Content-Length: 38
Date: Fri, 16 Oct 2026 05:36:59 GMT
{"id":2,"name":"Mobiles","parentId":1}
$ curl -i http://localhost:8080/api/v1/categories/99
HTTP/1.1 404
Content-Type: application/problem+json
Transfer-Encoding: chunked
Date: Fri, 16 Oct 2026 05:36:59 GMT
{"type":"https://shoplite.example/problems/not-found","title":"Resource not found","status":404,"detail":"Category 99 was not found.","instance":"/api/v1/categories/99"}
What this code does
- The operation
getunder/api/v1/categories/{id}takes one path parameter,id, anint64.GET /api/v1/categories/2is that operation withid= 2. - Response
"200"points to theCategoryschema:id,name,parentId, and the real body has exactly those fields.parentIdmay benull, whichtype: [integer, "null"]says. - Response
"404"usesapplication/problem+jsonand theProblemschema, matching the real error. JSON field order does not matter, so the server's order is fine.
paths lists the URL templates. responses is keyed by status code, written as a string ("200", "404"), and each one gives its media type and the body's schema: the fields, their types and which are required. A shared schema lives once under components.schemas and is pointed to with $ref, a reference such as "#/components/schemas/Category".
A parameter says where it travels with in: path (part of the URL, always required), query (after ?) or header. format: int64 means a Java long, and type: [integer, "null"] in OpenAPI 3.1 means the value may be null.
Predict the output
Read the spec
Read each piece of a ShopLite spec and pick the right answer.
Round 1 of 3
A client asks for a category id that does not exist.
Swagger UI: the spec as a web page you can try
Swagger UI is a free web page that loads an OpenAPI document and draws it: one row per operation, grouped by tag, with a Try it out button that sends real requests to the server.
Paste the spec into the Swagger Editor and its right side becomes Swagger UI. Below is the text of the page it draws.
Example 3 · The same spec in Swagger UI
Open: https://editor.swagger.ioDo: clear the left side, paste categories.yaml from Example 2Then: on the right, click the row GET /api/v1/categories/{id}ShopLite API v1 OAS 3.1
Servers
http://localhost:8080
Categories
GET /api/v1/categories/{id} Get one category
Parameters [Try it out]
Name Description
id * Category id
integer($int64) Example : 2
(path)
Responses
Code Description
200 The category
Media type: application/json
Example Value | Schema
{
"id": 2,
"name": "Mobiles",
"parentId": 1
}
404 No category has this id
Media type: application/problem+json
Example Value | Schema
{
"type": "https://shoplite.example/problems/not-found",
"title": "Resource not found",
"status": 404,
"detail": "Category 99 was not found.",
"instance": "/api/v1/categories/99"
}
Schemas
Category object
Problem objectWhat this code does
- The header shows the title, the
v1version badge andOAS 3.1(OpenAPI Specification 3.1); the Servers box shows where Try it out sends requests. - The row opens into Parameters (
id *, the star means required, with its example 2) and Responses, one block per status code with an example body built from the schema'sexamplevalues. - The Schemas list at the bottom shows every shape under
components.schemas; clicking one opens its fields and rules.
Each row has Try it out: you fill in the parameters, press Execute, and the page sends a real HTTP request to the server under Servers, then shows the status, headers and body. So the docs and a simple test client are the same page. A tag is a group name, such as Categories or Products.
The Swagger Editor at editor.swagger.io needs no install. The next lesson serves Swagger UI from ShopLite itself.
Code-first vs contract-first
Code-first means you write the controllers and a library generates the OpenAPI spec from them while the app runs. Contract-first (design-first) means you write and agree on the spec first, then build code that follows it.
Here is a ShopLite controller method with no docs annotations, and the spec springdoc generates from it.
Example 4 · Code-first: a controller method and the spec generated from it
Runs in: the shoplite-api Spring Boot project
// src/main/java/com/shoplite/category/CategoryController.java (from the earlier modules, no docs annotations)@RestController@RequestMapping("/api/v1/categories")class CategoryController {
private final CategoryService service;
CategoryController(CategoryService service) { this.service = service; }
@GetMapping("/{id}") CategoryResponse get(@PathVariable Long id) { return service.get(id); }
// list, create, replace and delete are unchanged} /api/v1/categories/{id}:
get:
tags:
- category-controller
operationId: get_1
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64
responses:
"200":
description: OK
content:
'*/*':
schema:
$ref: "#/components/schemas/CategoryResponse"
...
components:
schemas:
CategoryResponse:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
parentId:
type: integer
format: int64What this code does
- Nothing was written by hand: the path, the
idpath parameter and theCategoryResponseschema all come from the Java code. - Several things are guesses or wrong for ShopLite: the tag is the class name
category-controller, the operation id isget_1, the body type is*/*and there is no 404 response. - The next lessons fix each of these, so the generated spec says the same as the hand-written contract in Example 2.
Code-first is quick and never describes an endpoint that does not exist. But the generator only knows what the code tells it, so it guesses: it names the operation get_1, says success is 200 and lists no error. Contract-first gives the best design discussion, but code can drift from the spec unless a test or a code generator keeps them together.
ShopLite does both in a practical way: API-DESIGN.md was the contract written first, and from the next lesson the spec is generated from the code (springdoc-openapi), corrected with annotations and checked by a test.
Who uses the docs: frontend developers, testers, other teams
An API consumer is a person or program that calls your API without reading your Java code, such as a frontend app, a tester or another team's service.
Each group of consumers uses the same spec for a different job. A tester, for example, lists every operation to plan one test per response code.
# list the paths and operation names in a specgrep -E "^ /|operationId" categories.yaml- Frontend and mobile developers: the ShopLite web shop and Android app read field names, required fields and error shapes, and build screens before the server is finished.
- Testers: they try every status code from Swagger UI or a Postman collection made from the spec.
- Other teams and partners: a delivery or payment partner integrates without a meeting.
- Tools: Postman imports the spec, code generators write client classes from it, and tests compare it with the contract.
Next step
Check your understanding
12 quick questions