Create Simple Java RESTful Web Services Using Jersey

Create Simple Java RESTful Web Services Using Jersey

In this instructional exercise, you will figure out how to create simple Java RESTful web services using Jersey structure.

JAX-RS is the Java API utilized for making RESTful web services. There are for the most part two sorts of implementation of JAX-RS that are Jersey and RESTeasy. In this instructional exercise, we will see Jersey implementation.

We will utilize overshadowing to create our web administration. Ensure it contains apache tomcat server. You can pursue beneath connection to realize how to download and design tomcat.

Create Simple Java RESTful Web Services Using Jersey

Step by step instructions to Create Java RESTful Web Services Using Jersey Framework

  • Most importantly download the Jersey containers from beneath connect.

https://eclipse-ee4j.github.io/jersey/

Open obscuration and go to File – > New – > Dynamic Web Project to create a unique web venture with name JavaRESTfullWS.

Presently we need to import jersey jolts in our task. The document that you have downloaded in past advance is packed record.

Subsequent to separating it you will get a few organizers. Simply import every one of the containers present in those organizers.

You can pursue underneath interface in the event that you don’t have the foggiest idea how to impart jolts in obscure.

http://stackoverflow.com/questions/4440479/how-might I-add-container records to-the-web-inf-lib-organizer in-obscure

Create a package with name example under src organizer in your venture. Under this bundle create a source record with name DemoService and include the following code inside it.

DemoService.java

package example;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
 
@Path("/DemoService")
public class DemoService {
		
	@GET
	@Path("/{msg}")
	@Produces(MediaType.TEXT_HTML)
	public String printMsg(@PathParam("msg") String msg){
		return "<h1>Your message: "+msg+"</h1>";
	}	
}

Enter following code in web.xml record. You can discover it under Web-Content – > WEB-INF organizer.

On the off chance that it isn’t there, at that point create one.

Web.xml

<?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">  
 
 <servlet>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param>  
        <param-name>jersey.config.server.provider.packages</param-name>  
        <param-value>example</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
  </servlet-mapping>  
</web-app>

The undertaking has the following structure.

Create Simple Java RESTful Web Services Using Jersey

Right snap on your undertaking envelope and afterwards select Run As – > Run on Server.

For testing the web administration open after URL in the program.

http://localhost:8080/JavaRESTfullWS/rest/DemoService/Hello You can change Hello toward the finish of the URL to pass some other message as parameter.

Create Simple Java RESTful Web Services Using Jersey

Video Tutorial

You can watch underneath the video. Everything is clarified in it bit by bit.

Remark beneath in the event that you are confronting any trouble to create Java RESTful web services.

Cheerful Coding!! ????

Leave a Comment

error: Alert: Content is protected!!