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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. RESTful Web Services With Spring Boot, Gradle, HATEOAS, and Swagger

RESTful Web Services With Spring Boot, Gradle, HATEOAS, and Swagger

Let's look at RESTful web services with Spring Boot, Gradle, HATEOAS, and Swagger.

Jahid Akhtar user avatar by
Jahid Akhtar
·
Jul. 30, 19 · Opinion
Like (9)
Save
Tweet
Share
18.89K Views

Join the DZone community and get the full member experience.

Join For Free

Webservices are services that are exposed to the Internet for programmatic access. Those are online APIs that we can call from our code.

Additionally, REST stands for the Representational State Transfer. It's basically an architectural set of guidelines. REST uses already existing HTTP concepts. Because of this, we can use this style of architecture to implement web services. And when we apply REST to our web services, it's called RESTful Web Services.

Even though there are no strict rules, our RESTful web services can be determined by the Richardson Maturity Model. It's a guideline for designing RESTful APIs. As developers, while developing RESTful web services, we should make is as RESTful as possible.

Richardson Maturity Model

Level 0: No HTTP concept is being used, like SOAP, the same URL for many operations. Operations are defined in the message itself.

Level 1: Resource URIs. Individual URI for each resource

Level 2: HTTP methods and status codes are used appropriately

Level 3: Using HATEOAS, ie. responses have links that clients can use

HATEOAS: Hypermedia As The Engine Of Application State

HATEOAS is a component of the REST application architecture that distinguishes it from other network application architectures. With HATEOAS, a client interacts with a network application whose application servers provide information dynamically through hypermedia. A REST client needs little to no prior knowledge about how to interact with an application or server beyond a generic understanding of hypermedia.

Advantages of HATEOAS

  • Required APIs could be easily found without any specific documentation
  • Make the response navigable with links to common resource URIs
  • Get the message URL of the response itself
  • If any URL changes from the server-side, there's no need to do any change on the client-side
  • If we follow proper HATEOAS, the client actually doesn't need to spend much time understanding API documentation. As an end-user, we don't have to go through documentation every time we visit a new website. We just go to the homepage and go through the links. With HATEOAS, the web service is super helpful to the clients.

HATEOAS is very important in implementing web services. But there's a big problem because different developers can implement HATEOAS in various different ways. I mean, they can structure the JSON response in any format they want, and it can lead to a lot of confusion, and the main objective of HATEOAS wouldn't be achieved. Developers would have to learn how to use various different formats. Some standardization would really simplify the use of web services, particularly open source libraries.

HAL: Hypertext Application Language

HAL is one of the simplest and most widely adopted hypermedia media types adopted when not discussing specific web stacks. It was the first spec-based media type adopted by Spring HATEAOS. In HAL, the _links entry is a JSON object. The property names are link relations, and each value is either a link object or an array of link objects.

HAL is an Internet Draft (a "work in progress") standard convention for defining hypermedia such as links to external resources within JSON or XML code (however, please note that the latest version of HAL Internet-Draft expired on November 12, 2016.). The standard was initially proposed on June 2012, specifically for use with JSON[1] and has since become available in two variations, JSON and XML. The two associated MIME types are media type: application/hal+xml and media type: application/hal+json.

Swagger

Swagger is one of the most popular document format for RESTful webservices. Using Swagger developers can provide very rich API documentation with minimum effort. Also, using Swagger we can test the RESTful APIs, as it comes with inbuild HTTP client.

In this example, we will be using Spring Boot, Gradle, Java (version 8 or above), HATEOAS, and Swagger. Let's understand the APIs we are developing. It's a simple Book application. For simplicity, we won't be using any database, we will create some data in the code itself.

Book can have the following basic properties:

  • bookId
  • isbn
  • title
  • author
  • priceInINR

Below are the APIs that we are going to implement:

  • Retrieve all books: GET /books/all
  • Create a book: POST /books
  • Retrieve a particular book by ISBN: GET /books/isbn/{isbn}
  • Retrieve books by author: GET /books/author/{author}

In the "/books/all" API response for individual books, it would have a link to itself.

Image title

Request body for creating a book:

{
"isbn": "1234567891",
"title": "Book Title-2",
"author": "Author-1",
"genre": "Thriller",
"priceInINR": 900
}

Response:

Image title

Just check we have a link to the book created in the API response.

Similarly for the API "/books/isbn/{isbn}", in the response, there's a link to retrieve all the books by the same author. Below is a sample response:

Image title

Below is the response for the API "/books/author/{author}":

Image title

Finally, let's understand Swagger.

Below are the Gradle dependencies required for the application:

Image title

Below are the two Swagger URLs we can use:

http://localhost:9090/v2/api-docs

Image title

http://localhost:9090/swagger-ui.html

Image title

Image title

Image title

As you can see, Swagger creates quite a rich API documentation. There are a lot of options in Swagger API to enhance the API documentations. I've simply used a few of them. You can download the code from GitHub.

Source Code

Web Service REST Web Protocols application Spring Framework Gradle Book Links API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Asynchronous Messaging Service
  • Accelerating Enterprise Software Delivery Through Automated Release Processes in Scaled Agile Framework (SAFe)
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • Keep Your Application Secrets Secret

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: