0% found this document useful (0 votes)
77 views

Rest Webservice Using Jersey (Provider and Consumer)

The document discusses creating REST web services using Jersey provider and consumer in Java. It provides code samples and instructions for setting up a provider project that implements REST endpoints and a consumer project that calls the provider endpoints.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Rest Webservice Using Jersey (Provider and Consumer)

The document discusses creating REST web services using Jersey provider and consumer in Java. It provides code samples and instructions for setting up a provider project that implements REST endpoints and a consumer project that calls the provider endpoints.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

RAGHU SIR [Sathya Technologies, Ameerpet]

Rest Webservice using Jersey (Provider


and Consumer)
JDK 8: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-
2133151.html

Eclipse IDE (With STS): (32 bit os) :


http://download.springsource.com/release/STS/3.9.4.RELEASE/dist/e4.7/spring-tool-suite-
3.9.4.RELEASE-e4.7.3a-win32.zip
(64 bit os): http://download.springsource.com/release/STS/3.9.4.RELEASE/dist/e4.7/spring-tool-
suite-3.9.4.RELEASE-e4.7.3a-win32-x86_64.zip
Apache Tomcat : http://redrockdigimark.com/apachemirror/tomcat/tomcat-
8/v8.5.31/bin/apache-tomcat-8.5.31.exe

Provider Folder System:

1
RAGHU SIR [Sathya Technologies, Ameerpet]

1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sathaytech</groupId>
<artifactId>RestJerseyExMaven</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>RestJerseyExMaven Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19</version>
</dependency>

</dependencies>
<build>
<finalName>RestJerseyExMaven</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2. web.xml

<!DOCTYPE web-app PUBLIC


"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_5.dtd" >

2
RAGHU SIR [Sathya Technologies, Ameerpet]

<web-app>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-
class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet
-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

3. Service Provider class code

package com.app.provider;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;

@Path("/message")
public class MessageServiceProvider {

@Path("/show")
@GET
public String showGetMsg() {
return "Hello:GET";
}

@Path("/show")
@POST
public String showPostMsg() {
return "Hello:POST";
}

@Path("/show")
@PUT
public String showPutMsg() {
return "Hello:Put";
}

3
RAGHU SIR [Sathya Technologies, Ameerpet]

@Path("/show")
@DELETE
public String showDeleteMsg() {
return "Hello:DELETE";
}

@Path("/show")
@HEAD
public String showHeadMsg() {
return "Hello:NO-BODY";
}

@Path("/show")
@OPTIONS
public String showOptionsMsg() {
return "Hello:OPTIONS";
}

* After coding provider project Run in Server (Right click on Project > Run As > Run on Server)

-------------------------------------------------------------------------------------------------------------------------------------

Consumer Folder System:

1. pom.xml

4
RAGHU SIR [Sathya Technologies, Ameerpet]

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sathyatech</groupId>
<artifactId>RestJerseyConsumer</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

2. Client Application Code

package com.app;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class ClientApp {

public static void main(String[] args) {


Client c=new Client();
WebResource
r=c.resource("http://localhost:2018/RestJerseyExMaven/rest/message/s
how");
ClientResponse cr=r.get(ClientResponse.class);
String res=cr.getEntity(String.class);

5
RAGHU SIR [Sathya Technologies, Ameerpet]

System.out.println(res);
System.out.println(cr.getStatus());
System.out.println(cr.getStatusInfo());
System.out.println(cr.hasEntity());
System.out.println(cr.getType());

** Run Menu> Run option (or ctrl+F11)

Output:

FB Group: https://www.facebook.com/groups/thejavatemple/

email : [email protected]

You might also like