29

Implementing and Documenting REST APIs With Java EE

 5 years ago
source link: https://www.tuicool.com/articles/hit/ruuYRvr
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The standard way to implement REST APIs in Java EE applications is JAX-RS . The de-facto standard for API documentation is OpenAPI (formally Swagger). Eclipse MicroProfile supports developers using 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 Java EE 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 than 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.

package com.ibm.webapi.apis;

import javax.ws.rs.*;
import org.eclipse.microprofile.openapi.annotations.*;

@RequestScoped
@Path("/v1")
@OpenAPIDefinition(info = @Info(title = "Web-API Service", version = "1.0", description = "Web-API Service APIs", contact = @Contact(url = "https://github.com/nheidloff/cloud-native-starter", name = "Niklas Heidloff"), license = @License(name = "License", url = "https://github.com/nheidloff/cloud-native-starter/blob/master/LICENSE")))
public class GetArticles {
   @Inject
   com.ibm.webapi.business.Service service;
   @Inject
   ArticleAsJson articleAsJson;

   @GET
   @Path("/getmultiple")
   @Produces(MediaType.APPLICATION_JSON)
   @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 = articles.stream();
         Stream<JsonObject> streamJsonObjects = streamArticles.map(article -> {
            JsonObject output = articleAsJson.createJsonArticle(article);
              return output;
         });
         jsonArray = streamJsonObjects.collect(JsonCollectors.toJsonArray());
         return Response.ok(jsonArray).build();
      } catch (NoDataAccess e) {
         e.printStackTrace();
         return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
      }  
   }
}

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

mm67jeu.png!web

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK