GeezerVison is showing jooby.io — jooby: openapi module
Jooby
This module helps automating the generation of API documentation using Jooby projects. jooby-openapi works by examining an application at build time to infer API semantics based on byte code and optional annotations.
Automatically generates documentation in JSON/YAML format APIs. This documentation can be completed by javadoc comments or using swagger-api annotations.
<properties>
<application.class>myapp.App</application.class>
</properties>
...
<plugins>
...
<plugin>
<groupId>io.jooby</groupId>
<artifactId>jooby-maven-plugin</artifactId>
<version>4.6.0</version>
<executions>
<execution>
<goals>
<goal>openapi</goal>
</goals>
<configuration>
...
</configuration>
</execution>
</executions>
</plugin>
</plugins>
buildscript {
ext {
joobyVersion = "4.6.0"
}
}
plugins {
id "application"
id "io.jooby.openAPI" version "${joobyVersion}"
...
}
...
mainClassName = "myapp.App"
...
// Configuration
openAPI {
....
}
// Run openAPI task on joobyRun
joobyRun.dependsOn openAPI
// Run openAPI task on build
jar.dependsOn openAPI
|
Note
|
The default phase of the plugin execution is process-classes which means it will be executed on mvn jooby:run command and on hot reload.
It may slow down hot reload process in case of large projects with a lot of code to process.
To avoid this behaviour you can specify maven build phase which suits your needs better (e.g. prepare-package).
|
| Option |
Default Value |
Description |
adoc
|
|
List of asciidoc files used as template to generate documentation:
Maven:
<adoc>
<file>guide.adoc</file>
</adoc>
Gradle:
{
adoc = ["guide.adoc"]
}
|
basedir
|
${project.dir}
|
Set base directory used it for loading openAPI template file name.
|
copyOpenApiSpecTo
|
|
Copy the generated OpenAPI spec to the given file in the project repository. It must be one or
more directories or file names. In case of directory both the .json and .yaml file are copy.
Example:
Maven:
<copyOpenApiSpecTo>
<file>${project.basedir}/spec<file>
<file>${project.basedir}/docs/openapi.yaml<file>
</copyOpenApiSpecTo>
Gradle:
{
copyOpenApiSpecTo = ["${project.basedir}/spec", "$projectDir/docs/openapi.yaml"]
}
|
excludes
|
|
Regular expression used to excludes route. Example: /web.
|
includes
|
|
Regular expression used to includes/keep route. Example: /api/.*.
|
javadoc
|
on/true
|
Turn on/off javadoc generation from Java source files. Set to off or false to turn it off.
|
specVersion
|
3.0
|
Set the desired spec output. Possible values: 3.0 or 3.1
|
templateName
|
openapi.yaml
|
Set openAPI template file path.
|
To learn how it works, let’s write a simple Pet API:
{
install(new OpenAPIModule()); (1)
path("/pets", () { (2)
get("/", ctx -> {
PetRepository repo = ...;
return repo.list();
});
});
}
{
install(OpenAPIModule()) (1)
path("/pets") { (2)
get("/") {
val repo = ...;
repo.list()
}
}
}
-
Install OpenAPIModule
-
Write your API
The OpenAPIModule read from classpath the generated json and yaml files. To generate
them you need to build your project.
You will find the files in the output build directory. If your application is bar.Foo, then:
Maven
target/classes/bar/Foo.json
target/classes/bar/Foo.yaml
Gradle
build/classes/java/main/bar/Foo.json
build/classes/java/main/bar/Foo.yaml
|
Note
|
This is the main difference with previous version. We moved from runtime to build time generation. This way we:
-
Are able to get our OpenAPI files at build time (of course)
-
At runtime, we don’t waste resources (CPU, memory) while analyze and build the OpenAPI model
-
We keep bootstrap as fast as possible
|
The OpenAPI generator works exactly the same for MVC routes (a.k.a Controller):
{
install(new OpenAPIModule());
mvc(new Pets_());
}
/**
* Pet Library.
*/
@Path("/pets")
public class Pets {
/**
* List pets.
*
* @return All pets.
*/
@GET
public List<Pet> list() {
...
}
}
{
install(OpenAPIModule())
mvc(Pets_())
}
@Path("/pets")
class Pets {
@GET
fun list(): List<Pet> {
...
}
}
The Maven plugin and Gradle task provide two filter properties includes and excludes. These
properties filter routes by their path pattern. The filter is a regular expression.
Full/complete example available here
JavaDoc comments are supported for script and MVC routes on Java code base. They are enable by default, to turn them off set javadoc: off or javadoc: false in your maven/gradle plugin.
/**
* My Library.
* @version 5.4.1
* @tag Books. All about books.
* @server.url https://books.api.com
* @server.description Production
* @x-logo https://my.api.com/logo.png
*/
public class App extends Jooby {
{
install(new OpenAPIModule());
/*
* Books operations.
* @tag Books
*/
path("/api/books", () -> {
/*
* Query and filter books.
*
* @param query Book filter.
* @return List of matching books.
* @operationId query
*/
get("/", ctx -> {
var query = ctx.query(BookQuery.class);
});
/*
* Find a book by ISBN.
* @param isbn ISBN code.
* @return A book.
* @throws BookNotFoundException When a book doesn't exist. <code>404</code>
* @operationId bookByIsbn
*/
get("/{isbn}", ctx -> {
var query = ctx.query(BookQuery.class);
});
});
}
}
/**
* Book query.
*
* @type Book type.
* @aga Book age.
*/
public record BookQuery(BookType type, int age) {}
/**
* Books can be broadly categorized into fiction and non-fiction
*/
public enum BookType {
/**
* Fiction includes genres like fantasy, science fiction, romance, and mystery.
*/
Fiction,
/**
* Non-fiction encompasses genres like history, biography, and self-help.
*/
NonFiction
}
/**
* My Library.
* @version 5.4.1
* @tag Books. All about books.
* @server.url https://books.api.com
* @server.description Production
* @x-logo https://my.api.com/logo.png
*/
public class App extends Jooby {
{
mvc(new Books_());
install(new OpenAPIModule());
}
}
/**
* Books operations.
* @tag Books
*/
@Path("/api/books")
public class Books {
/**
* Query and filter books.
*
* @param query Book filter.
* @return List of matching books.
*/
public List<Book> query(@QueryParam BookQuery query) {
....
}
/**
* Find a book by ISBN.
*
* @param isbn ISBN code.
* @return A book.
* @throws BookNotFoundException When a book doesn't exist. <code>404</code>
*/
public Book bookByIsbn(@PathParam String isbn) {
}
}
/**
* Book query.
*
* @type Book type.
* @aga Book age.
*/
public record BookQuery(BookType type, int age) {}
/**
* Books can be broadly categorized into fiction and non-fiction
*/
public enum BookType {
/**
* Fiction includes genres like fantasy, science fiction, romance, and mystery.
*/
Fiction,
/**
* Non-fiction encompasses genres like history, biography, and self-help.
*/
NonFiction
}
A JavaDoc comment is split into:
Whitespaces (including new lines) are ignored. To introduce a new line, you must use a <p>.
| Tag |
Main |
Controller |
Method |
Description |
@version |
[x] |
|
|
|
@contact.name |
[x] |
|
|
|
@contact.url |
[x] |
|
|
|
@contact.email |
[x] |
|
|
|
@license.name |
[x] |
|
|
|
@license.url |
[x] |
|
|
|
@securityScheme.name |
[x] |
|
|
|
@securityScheme.in |
[x] |
|
|
|
@securityScheme.paramName |
[x] |
|
|
|
@securityScheme.flows.implicit.authorizationUrl |
[x] |
|
|
|
@securityScheme.flows.implicit.scopes.name |
[x] |
|
|
|
@securityScheme.flows.implicit.scopes.description |
[x] |
|
|
|
@securityRequirement |
|
[x] |
[x] |
Name of the securityScheme and optionally scopes. Example: myOauth2Security read:pets |
@server.description |
[x] |
|
|
|
@x- |
[x] |
[x] |
[x] |
Tag starting with x- is considered an extension |
@tag.name |
[x] |
[x] |
[x] |
Tag name |
@tag.description |
[x] |
[x] |
[x] |
Tag Description |
@tag |
[x] |
[x] |
[x] |
Shortcut for previous @tag.name and @tag.description. The tag name is everything before . |
@operationId <name> |
|
|
[x] |
Defined the operationId, useful for script routes. |
@param <name> |
|
|
[x] |
Operation parameter |
@return |
|
|
[x] |
Operation default response |
@throws |
|
|
[x] |
Additional non-success responses types. The HTTP response code is taken from <code>{number}</code>, or set to 500 error. |
This feature is only available for Java routes. Kotlin source code is not supported.
The OpenAPI output generates some default values for info and server section. It generates
the necessary to follow the specification and produces a valid output. These sections can be override
with better information/metadata.
To do so just write an openapi.yaml file inside the conf directory to use it as template.
conf/openapi.yaml
openapi: 3.0.1
info:
title: My Super API
description: |
Nunc commodo ipsum vitae dignissim congue. Quisque convallis malesuada tortor, non
lacinia quam malesuada id. Curabitur nisi mi, lobortis non tempus vel, vestibulum et neque.
...
version: "1.0"
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
paths:
/api/pets:
get:
operationId: listPets
description: List and sort pets.
parameters:
name: page
descripton: Page number.
All sections from template file are merged into the final output.
The extension property: x-merge-policy controls how merge must be done:
-
ignore: Silently ignore a path or operation present in template but not found in generated output. This is the default value.
-
keep: Add a path or operation to final output. Must be valid path or operation.
-
fail: Throw an error when path or operation is present in template but not found in generated output.
The extension property can be added at root/global level, paths, pathItem, operation or parameter level.
|
Note
|
Keep in mind that any section found here in the template overrides existing metadata.
|
Optionally, this plugin depends on some OpenAPI annotations. To use them, you need to add a dependency to your project:
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.55</version>
</dependency>
implementation 'io.swagger.core.v3:swagger-annotations:2.2.55'
Once you added to your project, you can annotate your routes:
Script
import io.swagger.v3.oas.annotations.Operation;
...
public class App extends Jooby {
{
path("/pets", () -> {
get("/{id}", this::findPetById)
});
}
@Operation(
summary = "Find a pet by ID",
description = "Find a pet by ID or throws a 404"
)
public Pet findPetById(Context ctx) {
PetRepo repo = require(PetRepo.class);
long id = ctx.path("id").longValue();
return repo.find(id);
}
}
import io.swagger.v3.oas.annotations.Operation
class App :Kooby({
path("/pets") {
get("/{id}", ::findPetById)
}
})
@Operation(
summary = "Find a pet by ID",
description = "Find a pet by ID or throws a 404"
)
fun findPetById(ctx: Context) : Pet {
val repo = ...
val id = ctx.path("id").longValue()
return repo.find(id)
}
The OpenAPI annotations complement the openAPI byte code parser by adding documentation
or being more specific about an operation, parameter, response type, response status, etc.
Annotations works as documentation but also as a way to override what was generated by the byte
code parser.
Annotations are supported at script routes (using the technique described before) and mvc routes.
If you look at the example, there is no documentation for path parameter: id, still this parameter
is going to be present in the OpenAPI files (present, but without documentation).
To add documentation just do:
@Operation(
summary = "Find a pet by ID",
description = "Find a pet by ID or throws a 404",
parameters = @Parameter(description = "Pet ID")
)
If the parameter annotation doesn’t specify a name, parameter binding follows a positional assignment.
This annotation is supported at the application level:
OpenAPIDefinition
@OpenAPIDefinition(
info = @Info(
title = "Title",
description = "description",
termsOfService = "Terms",
contact = @Contact(
name = "Jooby",
url = "https://jooby.io",
email = "[email protected]"
),
license = @License(
name = "Apache",
url = "https://jooby.io/LICENSE"
),
version = "10"
),
tags = @Tag(name = "mytag")
)
class App extends Jooby {
{
// All routes now have the default tag: `Pets`
}
}
@OpenAPIDefinition(
info = Info(
title = "Title",
description = "description",
termsOfService = "Terms",
contact = Contact(
name = "Jooby",
url = "https://jooby.io",
email = "[email protected]"
),
license = License(
name = "Apache",
url = "https://jooby.io/LICENSE"
),
version = "10"
),
tags = [Tag(name = "mytag")]
)
class App: Kooby({
})
Tagging is supported at three different levels:
Application level
@Tag(name = "Pets", description = "Pet operations")
class App extends Jooby {
{
// All routes now have the default tag: `Pets`
}
}
@Tag(name = "Pets", description = "Pet operations")
class App: Kooby({
// All routes now have the default tag: `Pets`
})
Controller level
@Tag(name = "Pets", description = "Pet operations")
@Path("/pets")
class Pets {
// All web method now have the default tag: `Pets`
}
@Tag(name = "Pets", description = "Pet operations")
@Path("/pets")
class Pets {
// All web method now have the default tag: `Pets`
}
Method level
@Tag(name = "Pets", description = "Pet operations")
public List<Pet> list(Context ctx) {
...
}
@Tag(name = "Pets", description = "Pet operations")
fun list(ctx: Context): List<Pet> {
...
}
For multiple tags use the @Tags annotation or the tags property of @OpenAPIDefinition
and/or @Operation annotations.
The default response code is Success(200) (or NO_CONTENT(204) for DELETE mvc routes). Now, if
you need to:
-
document the default response
-
use a custom response code
-
use multiple response codes
You need the ApiResponse annotation:
Document default response:
@ApiResponse(description = "This is the default response")
Use a custom response code:
@ApiResponse(responseCode = "201", description = "This is the default response")
Multiple response codes:
@ApiResponses({
@ApiResponse(description = "This is the default response"),
@ApiResponse(responseCode = "500"),
@ApiResponse(responseCode = "400"),
@ApiResponse(responseCode = "404")
})
1) create your template: doc/library.adoc
= 📚 {{info.title}} Guide
:source-highlighter: highlightjs
{{ info.description }}
== Base URL
All requests start with: `{{ server(0).url }}/library`
=== Summary
{{ routes | table(grid="rows") }}
...
<plugins>
...
<plugin>
<groupId>io.jooby</groupId>
<artifactId>jooby-maven-plugin</artifactId>
<version>4.6.0</version>
<executions>
<execution>
<goals>
<goal>openapi</goal>
</goals>
<configuration>
<adoc>
<file>doc/library.adoc</file>
</adoc>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
openAPI {
adoc = ["doc/library.adoc"]
}
3) The output directory will have two files:
- library.adoc (final asciidoctor file)
- library.html (asciidoctor output)
The Jooby OpenAPI Template Engine is a tool designed to generate comprehensive AsciiDoc (.adoc) documentation directly from your Jooby application’s OpenAPI model.
It uses pebble as a pre-processor to automate redundant tasks. Instead of manually writing repetitive documentation for every endpoint, you write a single template that pulls live data from your code (routes, schemas, javadoc).
You mix standard AsciiDoc text with Pebble logic.
-
{{ expression }}: Output. Use this to print values to the file.
Example: {{ info.title }} prints the API title.
-
{% tag %}: Logic. Use this for control flow (loops, variables, if/else) without printing output.
Example: {% for route in routes %} … {% endfor %} loops through your API routes.
Data generation follows a flexible pipeline architecture. You start with a source and can optionally transform it before rendering.
{{ Source | [Mutator] | Display }}
-
Source: Finds an object in the OpenAPI model (e.g., a route or schema).
-
Mutator (Optional): Transforms or filters the data (e.g., extracting just the body, or filtering parameters).
-
Display: Renders the final output (e.g., as JSON, a Table, or a cURL command).
These functions are your entry points to locate objects within the OpenAPI definition.
| Function |
Example |
Description |
operation(method, path) |
{{ operation("GET", "/books") }}
|
Generic lookup for an API operation. |
GET(path) |
{{ GET("/books") }}
|
Shorthand for operation("GET", path). |
POST(path) |
{{ POST("/books") }}
|
Shorthand for operation("POST", path). |
PUT / PATCH / DELETE |
{{ DELETE("/books/{id}") }}
|
Shorthand for respective HTTP methods. |
schema(name) |
{{ schema("User") }}
|
Looks up a Schema/Model definition by name. |
tag(name) |
{{ tag("Inventory") }}
|
Selects a specific Tag group (containing name, description, and routes). |
routes() |
{% for r in routes() %}…{% endfor %}
|
Returns a collection of all available routes in the API. |
server(index) |
{{ server(0).url }}
|
Selects a server definition from the OpenAPI spec by index. |
error(code) |
{{ statusCode(200) }}
{{ statusCode([200, 400]) }}
{{ statusCode( {200: "OK", 400: "Bad Syntax"} ) }}
|
Generates an error response object.
Default: {statusCode, reason, message}.
Custom: Looks for a global error variable map and interpolates values. |
{{ error(404) }}
|
statusCode(input) |
Generates status code descriptions. Accepts:
1. Int: Default reason.
2. List: [200, 404]
3. Map: {200: "OK", 400: "Bad Syntax"} (Overrides defaults). |
Mutators modify the data stream. They are optional but powerful for drilling down into specific parts of an object.
| Filter |
Description |
Input Context |
request
|
Extracts the Request component from an operation. |
Operation |
response(code)
|
Extracts a specific Response component.
Default: 200 (OK). |
Operation |
body
|
Extracts the Body payload definition. |
Operation / Request / Response |
form
|
Extracts form-data parameters specifically. |
Operation / Request |
parameters(type)
|
Extracts parameters.
Default: Returns all parameters.
Filter Arguments: query, header, path, cookie. |
Operation / Request |
example
|
Populates a Schema with example data. |
Schema / Body |
truncate
|
Takes a complex Schema and returns a new Schema containing only direct fields. Removes nested objects and deep relationships. |
Schema / Body |
The final step in the chain. These filters determine how the data is written to the AsciiDoc file.
| Filter |
Description |
curl
|
Generates a ready-to-use cURL command.
Includes method, headers, and body. |
http
|
Renders the raw HTTP Request/Response wire format.
(Status line, Headers, Body). |
path(params…)
|
Renders the full relative URI.
Arguments: Pass key=value pairs to override path variables or query parameters in the output.
Example: path(id=123, sort="asc") |
json
|
Renders the input object as a formatted JSON block. |
yaml
|
Renders the input object as a YAML block. |
table
|
Renders a standard AsciiDoc/Markdown table.
Great for lists of parameters or schema fields. |
list
|
Renders a simple bulleted list.
Used mostly for Status Codes or Enums. |
link
|
Renders an ascii doc on schema.
Only for Schemas. |
Use this pattern to document a specific endpoint, separating path parameters from query parameters.
// 1. Define the route
{% set route = GET("/library/books/{isbn}") %}
=== {{ route.summary }}
{{ route.description }}
// 2. Render Path Params
.Path Parameters
{{ route | parameters(path) | table }}
// 3. Render Query Params
.Query Parameters
{{ route | parameters(query) | table }}
// 4. Render Response + example
.Response
{{ route | response | body | example | json }}
// 5. Render Response and Override Status Code
.Created(201) Response
{{ route | response(201) | http }}
// 6. Render Response 400
.Bad Request Response
{{ route | response(400) | http }}
{{ route | response(400) | json }}
Provide a copy-paste command for developers.
{{ POST("/items") | curl }}
Passing curl options
{{ POST("/items") | curl("-i", "-H", "'Accept: application/xml'") }}
Generate a bash source
{{ POST("/items") | curl(language="bash") }}
Use the path filter to inject specific values into the URL, making the documentation more realistic.
// Scenario: Searching for Sci-Fi books on page 2
GET {{ GET("/search") | path(q="Sci-Fi", page=2) }}
Output: GET /search?q=Sci-Fi&page=2
If your database entities have deep nesting (e.g., Book → Author → Address → Country), use truncate to show a summary view.
// Full Graph (Huge JSON)
{{ schema("Book") | example | json }}
// Summary (Flat JSON)
{{ schema("Book") | truncate | example | json }}
You can generate error examples using the standard format or a custom structure.
1. Default Structure
Generates a JSON with statusCode, reason, and message.
.404 Not Found
{{ error(404) | json }}
2. Custom Error Structure
Define a variable named error with a map containing your fields. Use {{code}}, {{message}}, and {{reason}} placeholders which the engine will automatically populate.
// Define the custom error shape once
{%- set error = {
"code": "{{code}}",
"message": "{{message}}",
"reason": "{{reason}}",
"timestamp": "2025-01-01T12:00:00Z",
"support": "[email protected]"
} -%}
// Now generate the error. It will use the map above.
.400 Bad Request
{{ error(400) | json }}
{
"code": 400,
"message": "Bad Request",
"reason": "Bad Request",
"timestamp": "2025-01-01T12:00:00Z",
"support": "[email protected]"
}
Automatically document your API by iterating over tags defined in Java.
{% for tag in tags %}
== {{ tag.name }}
{% for route in tag.routes %}
=== {{ route.summary }}
{{ route.method }} {{ route.path }}
{% endfor %}
{% endfor %}
As your template grows, use macros to create reusable UI components (like warning blocks or deprecated notices) to keep the main logic clean.
{# 1. Define the Macro at the top of your file #}
{% macro deprecationWarning(since) %}
[WARNING]
====
This endpoint is deprecated since version {{ since }}.
Please use the newer version instead.
====
{% endmacro %}
{# 2. Use it inside your route loop #}
{% if route.deprecated %}
{{ deprecationWarning("v2.1") }}
{% endif %}
If your API uses authentication (OAuth2, API Keys), the security property on the route contains the requirements.
{% if route.security %}
.Required Permissions
[cols="1,3"]
|===
|Type | Scopes
{# Iterate through security schemes #}
{% for scheme in route.security %}
{% for req in scheme %}
| *{{ loop.key }}* | {{ req | join(", ") }}
{% endfor %}
{% endfor %}
|===
{% endif %}
AsciiDoc supports internal anchors. You can automatically link a route’s return type to its full Schema definition elsewhere in the document.
{# 1. Create Anchors in your Schema Loop #}
{% for s in schemas %}
[id="{{ s.name }}"]
== {{ s.name }}
{{ s | table }}
{% endfor %}
{# 2. Link to them in your Route Loop #}
.Response Type
Returns a {{ route | response | link }} object.
To use swagger-ui just add the dependency to your project:
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-swagger-ui</artifactId>
<version>4.6.0</version>
</dependency>
implementation 'io.jooby:jooby-swagger-ui:4.6.0'
The swagger-ui application will be available at /swagger. To modify the default path, just call swaggerUI(String)
To use redoc just add the dependency to your project:
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-redoc</artifactId>
<version>4.6.0</version>
</dependency>
implementation 'io.jooby:jooby-redoc:4.6.0'
The redoc application will be available at /redoc. To modify the default path, just call redoc(String)
|