Implementing and documenting REST APIs with JavaEE

The standard way to implement REST APIs in JavaEE applications is JAX-RS. The de-facto standard for API documentation is OpenAPI (formally Swagger). Eclipse MicroProfile supports developers to use OpenAPI annotations for JAX-RS API implementations to document the APIs.

Check out the cloud-native starter repo on GitHub. It comes with multiple microservices, two of them have been implemented with JavaEE and MicroProfile.

Eclipse MicroProfile supports the OpenAPI v3 specification. The documentation lists all available annotations.

I like using annotations directly in the code to document APIs rather than using external configuration files. The closer the documentation stays together with the actual API implementation, the better and more accurate it will be.

In my sample I created one Java class per endpoint to reduce the lengths of the files, since the API documentation can become quite verbose.

When it comes to the design of APIs my personal taste is to provide business-logic-oriented APIs rather the CRUD operations on resources. The following sample shows the ‘web-api/v1/getmultiple‘ endpoint which internally utilizes several resources. It’s part of the API service that uses the BFF (backend for frontend) pattern. It provides the exact APIs web applications need and doesn’t expose the internally used resources.


import org.eclipse.microprofile.openapi.annotations.*;

@OpenAPIDefinition(info = @Info(title = "Web-API Service", version = "1.0", description = "Web-API Service APIs", contact = @Contact(url = "", name = "Niklas Heidloff"), license = @License(name = "License", url = "")))
public class GetArticles {
   @Inject service;
   ArticleAsJson articleAsJson;

   @APIResponses(value = { 
      @APIResponse(responseCode = "200", description = "Get most recently added articles", content = @Content(mediaType = "application/json", schema = @Schema(type = SchemaType.ARRAY, implementation = Article.class))),			
      @APIResponse(responseCode = "500", description = "Internal service error") })
   @Operation(summary = "Get most recently added articles", description = "Get most recently added articles")
   public Response getArticles() {
      JsonArray jsonArray;
      try {
         List<Article> articles = service.getArticles();
         Stream<Article> streamArticles =;
         Stream<JsonObject> streamJsonObjects = -> {
            JsonObject output = articleAsJson.createJsonArticle(article);
              return output;
         jsonArray = streamJsonObjects.collect(JsonCollectors.toJsonArray());
         return Response.ok(jsonArray).build();
      } catch (NoDataAccess e) {
         return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

The nice thing about MicroProfile and the open source JavaEE application server OpenLiberty is that it comes with the built-in OpenAPI web application to read the documentation and to invoke the APIs.


To learn more about MicroProfile OpenAPI, check out the cloud-native starter repo and the documentation.