In order to demonstrate the support of Code-First by Eclipse MicroProfile,
the MicroProfile Starter Page can be visited.
After selecting MicroProfile Version 4.0, Wildfly Runtime, and at least
check OpenAPI, you can download a project skeleton as zipped archive.
The downloaded project skeleton comes with some sample resources already.
Here for example parts of the JAX/RS resource class:
import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition;
import org.eclipse.microprofile.openapi.annotations.info.Info;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
@Path("/booking")
@ApplicationScoped
@OpenAPIDefinition(info = @Info(title = "Booking endpoint", version = "1.0"))
public class BookingController {
@APIResponses(value = {
@APIResponse(
responseCode = "200",
description = "Booking for id",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(ref = "Booking")
)
),
@APIResponse(
responseCode = "404",
description = "No booking found for the id.")
})
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{bookingId}")
public Response getBooking(@PathParam("bookingId") String bookingId) {
...
}
}
In addition to the JAX/RS Java code, the annotations of the MicroProfile
OpenAPI package describe the REST API in more detail. The referenced
REST resource is also included in the sample code:
import org.eclipse.microprofile.openapi.annotations.media.Schema;
@Schema(name="Booking")
public class Booking {
@Schema(required = true, description = "Booking id")
private String id;
@Schema(required = true, description = "Description of the destination")
private Destination destination;
...
}
Without any further coding the sample project can be built and started with
-
unzip downloaded archive
-
change working directory of shell to the project root
-
build the sample project with mvn clean package
-
start the application with java -jar target/code-first-api-bootable.jar
(given you named the project code-first-api
)
The application offers an /openapi
endpoint, which can be requested
as follows:
$ curl localhost:8080/openapi
---
openapi: 3.0.3
info:
title: ROOT.war
version: "1.0"
servers:
- url: /
paths:
/booking/{bookingId}:
get:
parameters:
- name: bookingId
in: path
required: true
schema:
type: string
responses:
"200":
description: Booking for id
content:
application/json:
schema:
$ref: '#/components/schemas/Booking'
"404":
description: No booking found for the id.
/hello:
get:
responses:
"200":
description: OK
content:
'*/*':
schema:
type: string
components:
schemas:
Booking:
required:
- id
- destination
type: object
properties:
id:
description: Booking id
type: string
destination:
allOf:
- $ref: '#/components/schemas/Destination'
- description: Description of the destination
Destination:
type: object
properties:
country:
type: string
city:
type: string
Do you recognize the definitions from the sample code? Beside the
introspected JAX/RS definitions, the annotation values have also been
collected and assembled into a standard OpenAPI specification.