OpenAPI / Swagger for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is OpenAPI / Swagger and why does a Java developer need it?

\n\n

OpenAPI (formerly known as Swagger) is a specification for describing, creating, documenting, and consuming RESTful APIs. For a Java developer, OpenAPI / Swagger libraries are an indispensable tool that automates the process of API documentation and client code generation.

\n\n

Main reasons to use OpenAPI / Swagger in Java projects:

\n
    \n
  • Automatic documentation generation — the library analyzes your code (Spring Boot annotations, JAX-RS) and creates interactive documentation in Swagger UI format.
  • \n
  • Standardization — a unified API description format understandable to both humans and machines.
  • \n
  • Client code generation — you can generate a Java client for any API described in OpenAPI.
  • \n
  • Testing — Swagger UI allows you to make requests to the API directly from the browser.
  • \n
  • CI/CD integration — you can verify API compliance with the specification at the build stage.
  • \n
\n\n

In this article, we will look at two main Java libraries: springdoc-openapi (for Spring Boot) and Swagger Core (for JAX-RS and other frameworks).

\n\n

Installing the OpenAPI / Swagger library for Java

\n\n

Installation depends on the framework used. Let's look at two main options.

\n\n

Option 1: Spring Boot (springdoc-openapi)

\n

Add the dependency to your pom.xml (Maven):

\n
<dependency>\n    <groupId>org.springdoc</groupId>\n    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>\n    <version>2.6.0</version>\n</dependency>
\n\n

For Gradle (build.gradle):

\n
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
\n\n

Option 2: JAX-RS / Jakarta (Swagger Core)

\n

Maven:

\n
<dependency>\n    <groupId>io.swagger.core.v3</groupId>\n    <artifactId>swagger-jaxrs2-jakarta</artifactId>\n    <version>2.2.22</version>\n</dependency>
\n\n

Gradle:

\n
implementation 'io.swagger.core.v3:swagger-jaxrs2-jakarta:2.2.22'
\n\n

Quick start: first example with Spring Boot

\n\n

After adding the springdoc-openapi dependency to a Spring Boot project, Swagger UI is automatically available at http://localhost:8080/swagger-ui.html. Let's create a simple controller:

\n\n
import io.swagger.v3.oas.annotations.Operation;\nimport io.swagger.v3.oas.annotations.tags.Tag;\nimport org.springframework.web.bind.annotation.*;\n\n@RestController\n@RequestMapping("/api/users")\n@Tag(name = "User Controller", description = "User management")\npublic class UserController {\n\n    @GetMapping("/{id}")\n    @Operation(summary = "Get user by ID")\n    public String getUser(@PathVariable Long id) {\n        return "User with ID: " + id;\n    }\n\n    @PostMapping\n    @Operation(summary = "Create a new user")\n    public String createUser(@RequestBody String userName) {\n        return "Created user: " + userName;\n    }\n}
\n\n

After starting the application, open http://localhost:8080/swagger-ui.html — you will see interactive documentation with the ability to make requests.

\n\n

Main methods and annotations of OpenAPI / Swagger for Java

\n\n

Let's look at 10 key annotations and methods from the io.swagger.v3.oas.annotations library.

\n\n

1. @Operation

\n

What it does: Describes a single endpoint (API method). Adds a description, summary, tags, and operation ID.

\n

Parameters:

\n

Recommendations