Skip to lesson

What OpenAPI and Swagger UI are

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

JSON
{  "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" }        }      }    }  }}
The same document 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

What this code does

  • openapi names the spec version, info gives the API's title and version, and paths holds 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.

OpenAPI used to be called Swagger. In 2016 the Swagger specification was given to the OpenAPI Initiative and renamed; "Swagger" now names a set of tools, such as Swagger UI.

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

YAML
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
Sample output: what the running ShopLite API answers
$ 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 get under /api/v1/categories/{id} takes one path parameter, id, an int64. GET /api/v1/categories/2 is that operation with id = 2.
  • Response "200" points to the Category schema: id, name, parentId, and the real body has exactly those fields. parentId may be null, which type: [integer, "null"] says.
  • Response "404" uses application/problem+json and the Problem schema, 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.

/api/v1/categories/{id}:
get:
responses:
"200":
description: The category
"404":
description: No category has this id

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

Text
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}
The page Swagger UI draws (its text)
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    object

What this code does

  • The header shows the title, the v1 version badge and OAS 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's example values.
  • 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.

Example values in a spec are only examples. Swagger UI shows them as hints and fills them in for Try it out; they are not checked against the real server.

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

Java
// 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}
Generated by springdoc-openapi (set up in the next lesson), trimmed
  /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: int64

What this code does

  • Nothing was written by hand: the path, the id path parameter and the CategoryResponse schema 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 is get_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.

Terminal
# 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.
One OpenAPI spec, many readers. The spec is one JSON file served at /v3/api-docs. Swagger UI shows it as a page with Try it out, Postman imports it as ready requests, frontend and mobile teams read field names and error shapes from it, and a docs test checks it against the contract.
Interview angle: "How do other teams know how to call your API?" A good answer names the OpenAPI spec, Swagger UI for trying it, and how you keep the spec true.

Next step

Check your understanding

12 quick questions