Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (54 sloc) 2.73 KB

Demo: Expose REST APIs

MicroProfile supports defining REST APIs via JAX-RS.

The following sample shows the ‘web-api/v1/getmultiple‘ endpoint:

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();

When using Open Liberty to run the MicroProfile code, you get an the OpenAPI explorer out of the box:

Invoke the following commands to set up the demo. Skip the commands you've already executed.

$ scripts/
$ scripts/
$ scripts/
$ scripts/
$ scripts/
$ scripts/

Invoke the curl command of the 'web-api' microserivce which is displayed as output of 'scripts/' to get ten articles, for example ''.

Check out the article Implementing and documenting REST APIs with JavaEE for more details.

You can’t perform that action at this time.