Skip to content

Commit 11740f1

Browse files
authored
Refactor OpenAPI documentation and update dependency version
Reorganize content on generating OpenAPI documents and improve clarity on annotation-based generation. Update version number for microprofile-openapi-api dependency.
1 parent a291209 commit 11740f1

1 file changed

Lines changed: 16 additions & 136 deletions

File tree

modules/ROOT/pages/chapter04/chapter04.adoc

Lines changed: 16 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,21 @@ MicroProfile OpenAPI ensures your documentation stays synchronized with your imp
7272

7373
== Generating OpenAPI documents
7474

75-
Add annotations to your Jakarta REST resources to generate documentation with MicroProfile OpenAPI. This annotation-driven approach keeps your documentation synchronized with your code. When you update an endpoint, you update its documentation at the same time.
75+
MicroProfile OpenAPI provides multiple approaches to generate OpenAPI documentation. You can use annotations, static files, or a combination of both to create comprehensive API specifications.
7676

7777
=== Annotation-based generation
7878

79-
You can use OpenAPI annotations to mark up your Jakarta REST classes and methods. MicroProfile OpenAPI then scans your application during the build time, automatically discovering the annotated endpoints, and producing a full OpenAPI specification.
79+
Add annotations to your Jakarta REST resources to generate documentation with MicroProfile OpenAPI. This annotation-driven approach keeps your documentation synchronized with your code. When you update an endpoint, you update its documentation at the same time.
80+
81+
Annotate your Jakarta REST classes and methods with OpenAPI annotations. MicroProfile OpenAPI scans your application at build time, discovers annotated endpoints, and generates a complete OpenAPI specification automatically.
8082

81-
This approach has several advantages:
83+
This approach offers several benefits:
8284

8385
* Documentation lives alongside the code it describes
84-
* IDEs can provide support for autocomplete and validation features.
86+
* IDE support for autocomplete and validation
8587
* Compile-time checking ensures annotations remain valid
8688
* No separate specification files to maintain
8789

88-
== Generating OpenAPI documents
89-
90-
There are multiple ways in which you can generate OpenAPI documents. The most common way is to use annotations. This only requires augmenting your Jakarta Restful Web Services annotations with OpenAPI annotations.
91-
92-
Besides annotations, a predefined OpenAPI document may be provided in either YAML or JSON format. This so-called static model will be merged with the model generated by scanning for Jakarta REST endpoints and the combined result will be made available to clients. However, the annotation-based approach is recommended as it is more maintainable and easier to understand. Finally, you can filter out the resources you do not want to document using configuration.
93-
9490
=== Static OpenAPI files (optional)
9591

9692
You can also provide a static OpenAPI document in `.yaml` or `.json` format at `META-INF/openapi.yaml` or `META-INF/openapi.json`. MicroProfile OpenAPI will merge this static content with the generated specification, which allows you to:
@@ -114,7 +110,7 @@ To use MicroProfile OpenAPI in your project, add the following Maven coordinates
114110
<dependency>
115111
<groupId>org.eclipse.microprofile.openapi</groupId>
116112
<artifactId>microprofile-openapi-api</artifactId>
117-
<version>4.0</version>
113+
<version>4.1</version>
118114
<scope>provided</scope>
119115
</dependency>
120116
----
@@ -124,22 +120,20 @@ To use MicroProfile OpenAPI in your project, add the following Maven coordinates
124120
Use `<scope>provided</scope>` because your MicroProfile runtime already includes the implementation.
125121
====
126122

127-
== Using MicroProfile Open API in your project
123+
=== Configuring annotation scanning
128124

129-
To document Jakarta RESTful Web Services using MicroProfile OpenAPI, we need to annotate the resource classes and methods with the OpenAPI annotation.
130-
131-
To use MicroProfile OpenAPI in your project, you need to add the following maven coordinates to your project:
125+
Add the following property to the `src/main/resources/META-INF/microprofile-config.properties` file:
132126

133-
[source, xml]
127+
[source, properties]
134128
----
135-
<dependency>
136-
<groupId>org.eclipse.microprofile.openapi</groupId>
137-
<artifactId>microprofile-openapi-api</artifactId>
138-
<version>3.1.1</version>
139-
</dependency>
129+
mp.openapi.scan=true
140130
----
141131

142-
Below is an illustrative example of how you might annotate a method in the `ProductResource` class to achieve this documentation using MicroProfile OpenAPI annotations:
132+
This property tells MicroProfile OpenAPI to scan your classes for annotations and generate API documentation for them.
133+
134+
== Basic annotation example
135+
136+
To document Jakarta RESTful Web Services using MicroProfile OpenAPI, annotate your resource classes and methods with OpenAPI annotations. The following example shows how to annotate a method in the `ProductResource` class:
143137

144138
[source, java]
145139
----
@@ -185,15 +179,6 @@ The annotations provide the following documentation:
185179

186180
These annotations enrich the `ProductResource` class with metadata necessary for generating comprehensive OpenAPI documentation automatically.
187181

188-
Add the following property to the `src/main/resources/META-INF/microprofile-config.properties` file:
189-
190-
[source, properties]
191-
----
192-
mp.openapi.scan=true
193-
----
194-
195-
This property tells MicroProfile OpenAPI to scan your classes for annotations and generate API documentation for them.
196-
197182
Build and run your application after configuring MicroProfile OpenAPI.
198183

199184
== Viewing the generated documentation
@@ -1430,111 +1415,6 @@ public class SecuredProductResource {
14301415
}
14311416
----
14321417

1433-
==== Applying security to operations
1434-
1435-
You can apply security requirements to individual operations or to entire resource classes.
1436-
1437-
*Applying security to individual operations:*
1438-
1439-
[source, java]
1440-
----
1441-
package io.microprofile.tutorial.store.product.resource;
1442-
1443-
import org.eclipse.microprofile.openapi.annotations.Operation;
1444-
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement;
1445-
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
1446-
import org.eclipse.microprofile.openapi.annotations.media.Content;
1447-
import org.eclipse.microprofile.openapi.annotations.media.Schema;
1448-
import jakarta.ws.rs.*;
1449-
import jakarta.ws.rs.core.MediaType;
1450-
import jakarta.ws.rs.core.Response;
1451-
1452-
@Path("/products")
1453-
public class SecuredProductResource {
1454-
1455-
@GET
1456-
@Path("/{id}")
1457-
@Produces(MediaType.APPLICATION_JSON)
1458-
@Operation(
1459-
summary = "Get product by ID",
1460-
description = "Retrieves detailed product information. Requires JWT authentication."
1461-
)
1462-
@SecurityRequirement(name = "bearerAuth")
1463-
@APIResponse(
1464-
responseCode = "200",
1465-
description = "Product found",
1466-
content = @Content(
1467-
mediaType = MediaType.APPLICATION_JSON,
1468-
schema = @Schema(implementation = Product.class)
1469-
)
1470-
)
1471-
@APIResponse(
1472-
responseCode = "401",
1473-
description = "Unauthorized - Missing or invalid JWT token"
1474-
)
1475-
@APIResponse(
1476-
responseCode = "404",
1477-
description = "Product not found"
1478-
)
1479-
public Response getSecuredProduct(@PathParam("id") Long id) {
1480-
// Security framework verifies JWT token
1481-
Product product = productService.findById(id);
1482-
1483-
if (product == null) {
1484-
return Response.status(Response.Status.NOT_FOUND).build();
1485-
}
1486-
1487-
return Response.ok(product).build();
1488-
}
1489-
1490-
@POST
1491-
@Consumes(MediaType.APPLICATION_JSON)
1492-
@Produces(MediaType.APPLICATION_JSON)
1493-
@Operation(
1494-
summary = "Create a new product",
1495-
description = "Creates a new product in the catalog. Requires OAuth2 write:products scope."
1496-
)
1497-
@SecurityRequirement(name = "oauth2", scopes = {"write:products"})
1498-
@APIResponse(
1499-
responseCode = "201",
1500-
description = "Product created successfully",
1501-
content = @Content(
1502-
mediaType = MediaType.APPLICATION_JSON,
1503-
schema = @Schema(implementation = Product.class)
1504-
)
1505-
)
1506-
@APIResponse(
1507-
responseCode = "401",
1508-
description = "Unauthorized - Missing or invalid OAuth2 token"
1509-
)
1510-
@APIResponse(
1511-
responseCode = "403",
1512-
description = "Forbidden - Token lacks required write:products scope"
1513-
)
1514-
@APIResponse(
1515-
responseCode = "400",
1516-
description = "Bad request - Invalid product data"
1517-
)
1518-
public Response createSecuredProduct(
1519-
@Schema(description = "Product to create", required = true)
1520-
Product product
1521-
) {
1522-
// Security framework verifies OAuth2 token and scopes
1523-
1524-
if (product == null || product.getName() == null) {
1525-
return Response.status(Response.Status.BAD_REQUEST)
1526-
.entity("Product name is required")
1527-
.build();
1528-
}
1529-
1530-
Product created = productService.create(product);
1531-
return Response.status(Response.Status.CREATED)
1532-
.entity(created)
1533-
.build();
1534-
}
1535-
}
1536-
----
1537-
15381418
==== Security in Swagger UI
15391419

15401420
When you view the documented API in Swagger UI, security schemes appear in several ways:

0 commit comments

Comments
 (0)