Debakar Roy

Building a Restful API using JAX-RS

2 min read

1. What are the tools needed?

2. Basic setup

3. Setting up Tomcat Server and Postman

4. Structure

5. Book Model

6. Book Database

7. Book Services

8. Book Resource

9. GET

10. POST

11. PUT

12. DELETE

13. Query

      -Get Book(s) by title

      -Get Book(s) by subject ID

Another thing we are going to use is called Postman. Postman is an HTTP Request composer. It helps you test your API in a very efficient way. You can download Postman from here.

Fill in the following data

Archetype Artifact Id: jersey-quickstart-webapp

Archetype Version: 2.16

Alternatively you can add the following dependency after creating your Maven Project.

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.archetypes/jersey-quickstart-webapp -->
<dependency>
    <groupId>org.glassfish.jersey.archetypes</groupId>
    <artifactId>jersey-quickstart-webapp</artifactId>
    <version>2.16</version>
</dependency>

Now you need to enter your project details. Mine are:

Group Id: org.debakar.bakadigest Artifact Id: bookDatabase

This will create a package org.debakar.bakadigest.bookDatabase where your resource files are stored.

The very first thing you need to do is open up your project and double-click pom.xml. Now switch to the pom.xml tab and uncomment the moxy dependency. This will let you produce JSON data. This is the same place where you can add the jersey dependency after creating a Maven Project using the maven archetype.

<dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
</dependency>

Postman doesn’t need any particular steps to install. You just install it like any other application, or add the Chrome app if you are using Chrome.

Here’s the project structure:

[Screenshot of the Eclipse project structure — original image link is dead]

[Screenshot of the Book model class — original image link is dead]

[Screenshot of the Book database class — original image link is dead]

NOTE: This is not a great way to do it for business purposes.

[Screenshot of the Book service class — original image link is dead]

[Screenshot of the Book resource class — original image link is dead]

[Screenshot of testing GET in Postman — original image link is dead]

[Screenshot of testing POST in Postman — original image link is dead]

[Screenshot of testing PUT, DELETE, and query endpoints in Postman — original image link is dead]