Table of contents
1.
Introduction
2.
RESTful - Application
2.1.
Making a Java Undertaking
2.2.
Adding the Expected Libraries
2.3.
Making the Source Records
2.4.
UserService.java
2.5.
Making the Web.xml design Record 💿
2.6.
Sending the Program
3.
Frequently Asked Questions
3.1.
What is a Relaxing Programming interface model?
3.2.
What contrasts the REST Programming interface and the Soothing Programming interface?
3.3.
What is a Serene framework?
3.4.
Why is Programming interface Serene?
3.5.
Is REST Programming interface frontend or backend?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

RESTful - Application

Author Adya Tiwari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey, Ninja🥷 A REST Programming interface (otherwise called Serene Programming Interface) is an application programming connection point (Programming interface or web Programming interface) that adjusts to the limitations of REST structural style and considers cooperation with Relaxing web administrations. REST represents an authentic state move made by PC researcher Roy Handling. 

A Relaxing Programming interface is a building style for an application program interface (Programming interface) that utilizes HTTP solicitations to access and use information. That information can be utilized to GET, PUT, POST, and Erase information types, which allude to the perusing, refreshing, making, and erasing of tasks concerning assets.

restful app CN

RESTful - Application

Allow us to begin composing the genuine Peaceful web administrations with Jersey Structure. Before you start writing your most memorable model utilizing the Jersey System, you must ensure that you have arranged your Jersey climate appropriately as made sense in the Relaxing Web Administrations.

Here, we expect you to have some working information on Obscuration IDE.

In this way, let us continue to compose a basic Jersey Application that will uncover a web administration strategy to show the rundown of clients.

Making a Java Undertaking

The initial step is to make a Unique Web Venture utilizing Shroud IDE. Follow the choice Record → New → Task. Lastly, select the Unique Web Venture wizard from the wizard list. Presently name your task as UserManagement involving the wizard window as displayed in the accompanying screen capture − 

java undertaking

When your undertaking is made effectively, you will have the accompanying substance in your Task Wayfarer

Adding the Expected Libraries

As a subsequent step, we add Jersey Structure and its conditions (libraries) in our task. Duplicate all containers from the following catalogs of download shirt zip organizer in WEB-INF/lib registry of the undertaking.

✳️\jaxrs-ri-2.17\jaxrs-ri\api

✳️\jaxrs-ri-2.17\jaxrs-ri\ext

✳️\jaxrs-ri-2.17\jaxrs-ri\lib

Right snap on your venture name UserManagement and follow the choice accessible in the setting menu − Assemble Way → Design Construct Way to show the Java Fabricate Way window.

Let us Add JARs button accessible under the Libraries tab to add the Containers present in WEBINF/lib catalog.

Making the Source Records

Presently let us make the genuine source documents under the UserManagement project. First, we want to make a bundle called com.codingninjas. To do this, right snap on src in the bundle adventurer segment and follow the choice − New → Bundle. 🗃️

Next, we will make UserService.java, User.java,UserDao.java documents under the com.codingninjas bundle. 

User.java

package com.codingninjas;  

import java.io.Serializable;  
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")

public class User implements Serializable {  
   private static final long serialVersionUID = 1L;
   private int id;
   private String name;
   private String profession;  
   public User(){}
   
   public User(int id, String name, String profession){  
      this.id = id;
      this.name = name;
      this.profession = profession;
   }  
   public int getId() {
      return id;
   }  
   @XmlElement
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   @XmlElement
   public void setName(String name) {
      this.name = name;
   }
   public String getProfession() {
      return profession;
   }
   @XmlElement
   public void setProfession(String profession) {
      this.profession = profession;
   }  
}
You can also try this code with Online Java Compiler
Run Code
gif CN

UserDao.java 🧑‍💻

package com.codingninjas;  

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;  

public class UserDao {
   public List<User> getAllUsers(){
     
      List<User> userList = null;
      try {
         File file = new File("Users.dat");
         if (!file.exists()) {
            User user = new User(1, "Mahesh", "Teacher");
            userList = new ArrayList<User>();
            userList.add(user);
            saveUserList(userList);
         }
         else{
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            userList = (List<User>) ois.readObject();
            ois.close();
         }
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }  
      return userList;
   }
   private void saveUserList(List<User> userList){
      try {
         File file = new File("Users.dat");
         FileOutputStream fos;  
         fos = new FileOutputStream(file);
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeObject(userList);
         oos.close();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }    
}
You can also try this code with Online Java Compiler
Run Code

 

UserService.java

package com.codingninjas;  

import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;  
@Path("/UserService")

public class UserService {  
   UserDao userDao = new UserDao();  
   @GET
   @Path("/users")
   @Produces(MediaType.APPLICATION_XML)
   public List<User> getUsers(){
      return userDao.getAllUsers();
   }  
}
You can also try this code with Online Java Compiler
Run Code

 

There are two significant focuses to be noted about the principal program,

UserService.java

🔅The initial step is to indicate a way for the web administration utilizing @Path explanation to the UserService.

🔅The subsequent step is to show a way for the specific web administration technique using @Path answer to strategy for UserService.

Making the Web.xml design Record 💿

You really want to make an Internet xml Setup record which is a XML document and is utilized to indicate the Jersey system servlet for our application.

<?xml version = "1.0" encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  
   xmlns = "http://java.sun.com/xml/ns/javaee"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
   id = "WebApp_ID" version = "3.0">
   <display-name>User Management</display-name>
   <servlet>
      <servlet-name>Jersey RESTful Application</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
         <param-name>jersey.config.server.provider.packages</param-name>
         <param-value>com.codingninjas</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>Jersey RESTful Application</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>  
</web-app>
program CN

Sending the Program

When you are finished making source and web arrangement records, you are prepared for this step, incorporating and running your program. To do this, utilizing Overshadowing, send out your application as a conflicting record and convey a similar in tomcat.

To make a Conflict record utilizing shroud, follow the choice Document → send out → Web → War Document; lastly, select venture UserManagement and objective envelope. To send a conflict document in Tomcat, place the UserManagement.war in the Tomcat Establishment Catalog → webapps registry and begin the Tomcat.

Frequently Asked Questions

What is a Relaxing Programming interface model?

A REST Programming interface utilizes a GET solicitation to recover a record, a POST solicitation to make one, a PUT solicitation to refresh a form, and an Erase solicitation to erase one.

What contrasts the REST Programming interface and the Soothing Programming interface?

Set forth plainly, there are no distinctions between REST and Serene, considering everything.

What is a Serene framework?

By utilizing a stateless convention and standard tasks, Relaxing frameworks go for the gold, unwavering quality, and the capacity to develop by reusing parts and refreshing without influencing the framework overall, even while it is running.

Why is Programming interface Serene?

One of the critical benefits of REST APIs is that they give a lot of adaptabilities.

Is REST Programming interface frontend or backend?

REST and GraphQL are both standard ways of creating backend APIs. Be that as it may, over the last ten years REST APIs have ruled as a decision for creating backend Programming interfaces.

Conclusion

In conclusion, A REST Programming interface is an application programming connection point that adjusts to the limitations of the REST structural style and considers cooperation with Relaxing web administrations. REST represents an authentic state move made by PC researcher Roy Handling. We know what makes a java undertaking, and the source record is detailed with respective code. 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in pygameCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass