DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Implementing and Documenting REST APIs With Java EE

Implementing and Documenting REST APIs With Java EE

Learn more about implementing REST APIs with Java EE.

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Mar. 19, 19 · Tutorial
Like (7)
Save
Tweet
Share
15.17K Views

Join the DZone community and get the full member experience.

Join For Free

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.

to learn more about microprofile openapi, check out the cloud-native starter repo and the documentation .

Java EE REST Web Protocols Java (programming language)

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top Five Tools for AI-based Test Automation
  • Stream Processing vs. Batch Processing: What to Know
  • A Real-Time Supply Chain Control Tower Powered by Kafka
  • Promises, Thenables, and Lazy-Evaluation: What, Why, How

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: