RESTful Tutorial


Developing RESTful applications using Netbeans 8

Software Requirements
Netbeans 8.0
Apache Tomcat 7
JDK 8

This document will guide you through the step by step development of a RESTful application called RESTDemo which will create services for handling Account. Here Account is a domain object and we will create the RESTful services for creating, updating, deleting, finding the account by id and list out all the accounts.

Steps:

  1.  Create a java web application (In our case project name is RESTDemo)
  2.  Once the project is created test it by simply running and showing the index page.
  3.  Select the root node of project, right click and select Properties option. A dialog will be appear on the screen to configure the project.
  4. Select the Libraries node from the left side Categories tree.
  5. And click on the Add Library... button to import the libraries into our project.
  6. Again a dialog box will appear called Add Library click on Import... button
  7. There are a list of in-built libraries in the tree select the two libraries called JAX-RS 2.0 and Jersey 2.5.1 (JAX-RS RI) And click import Add Library OK
  8. Open the web.xml file from projects WEB-INF folder if its not there then create a web.xml file and make the require changes. Here is our web.xml file looks like.


 

Now our project is ready to create and run the RESTful Services.

Lets create the RESTful Services for handling the Account objects.
  •  Create a POJO called Account
 
Note: Create the getter and setters as well.


  •  Just create a simple class called AccountResource to work on Account domain object.
 
Note: Within the default constructor we are initializing our AccountDAO class for calling the database operation. You can initializing any classes to call their methods from this AccountResource class.

Annotate the class with @Path and give it path name called “account” so that whatever the action we have to take on the account we will go through this path. And actually this is the root of the path or URL for the actions we are going to define and finally will invoke them.

✔ List out all the Accounts

We are going to create a REST service to list out all the accounts. Let's create a method call
getAllAccounts() with no parameter with List return type within the AccountResource
class. Because we just want to list out all the account we do not have to send any parameter with the
request so we will make a GET request. For making a GET request we will mark this method
with @GET annotation. We will have to give a path name to this method so that server can find and
reach this method. So we will mark this method with @Path annotation and will pass a value called
findAll”. To get the response in JSON format we have to declare the method with @Produces(value = “application/json”) annotation. Now our method look like this:



Now deploy the application and hit the URL in your browser: http://localhost:8080/RESTDemo/resources/account/findAll

Output should be something like this:

✔ Find a particular Account by Id

We are going to create a REST service to find or get the particular Account object. Let's create method
called getAccountById(long accountId) with a parameter long accountId and return type as Account within the AccountResource class. Again we are going to make a GET request but this time with a parameter called id. Concept is same as defined for the above method but we have one additional thing that we have notice here is @PathParam.
  •  Please pay attention to the value of the @Path(value = “find/{id}”). Here we define the path name find with a parameter called id. This is not a Query-string parameter but this is a path parameter in REST style.
  • Now please pay attention to the getAccountById(@PathParam(value = “id”) long accountId). The parameter long accountId we have defined is being bind with the {id} parameter.
 
Now deploy the application and hit the URL in your browser: http://localhost:8080/RESTDemo/resources/account/find/1
Output should something like this:

✔ Create a new Account

We are going to create a REST service to create or add a new Account to the target resource that could
be a anything like a table in a database, a record in a file on file server or anything depends on the
requirement. In our case its a dummy database.

Let's create a method called createAccount(Account account) with a parameter Account and return
type as Account. Define the path name for this method as “create” using the @Path annotation.
Syntax would be @Path(value = “create”). Because we are going to send some data with the request
in the request body we will mark this method as either @PUT or @POST annotation. You will have to google to find the differences between PUT and POST for RESTful services. Although as per my
understanding using @PUT is more useful than @POST. I am going to use @PUT annotation. There is a new annotation is going to be used is @Consumes. @Consumes defines the format of data in the request body. This is our create method:




Now deploy the application. Because its a PUT request we directly cannot hit this URL from our
browser address bar. Although there are plug-ins available for almost all the famous browser to make
a POST or PUT request using the tool. But don't worry we will have a look on this in section of
Invoking the REST services from Client.



✔ Update an Account
Let's create a method called updateAccount(Account account) with a parameter Account and return
type as Account. Define the path name for this method as “update” using the @Path annotation.
Syntax would be @Path(value = “update”).


Deploy the application.

✔ Create the REST Client

1. Create a Java application project call RestClient using Netbeans.

2. Select the root node of the project and right click

3. Go to New option and click Other... option

4. Select the Web Services from in the categories box

5. From the File types box select the RESTful Java Client and click Next

6. Enter the Class Name as AccountClient

7. Enter the Package name. (The AccountClient generated by netbeans will be there in this package)

8. Click on the Browse button

9. Select and expand the RESTDemo

10. Select the AccountResource and click OK

11. Click Finish.

Generated class would be something like this:




Note: change the BASE_URI in this class. Because we have mention the url-pattern in the web.xml to the resources and Netbeans generate by default as webresources.

Change it from "http://localhost:8080/RESTDemo/webresources” to
"http://localhost:8080/RESTDemo/resources"


✔ Invoking the Rest client
1. Copy the domain object Account from RESTDemo app to RestClient app.
2. Create a class called RestClient with a main method like below and run.



Output is something like this:


Thank You!

Comments

Popular posts from this blog

Java 21 Features With Example

Java 10 and 10 Big Java Milestones