Configuring the hello web service using REST web services
Download following api
jersey-bundle-1.19.jar
http://repo1.maven.org/maven2/com/sun/jersey/jersey-bundle/1.19/jersey-bundle-1.19.jar
Create project with name: TestWeb
Made changes into 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>TestWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Made changes into Java class
package com.test.rest.service;
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.Response;
@Path("/user-management")
public class TestRestService
{
@GET
@Path("/users")
public Response getAllUsers()
{
String result = "<h1>Web Application</h1>In real world application, a collection of users will be returned !!";
return Response.status(200).entity(result).build();
}
}
Output:
Output from Server ....
<h1>Web Application</h1>In real world application, a collection of users will be returned !!
Download following api
jersey-bundle-1.19.jar
http://repo1.maven.org/maven2/com/sun/jersey/jersey-bundle/1.19/jersey-bundle-1.19.jar
Create project with name: TestWeb
Made changes into 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>TestWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Made changes into Java class
package com.test.rest.service;
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.Response;
@Path("/user-management")
public class TestRestService
{
@GET
@Path("/users")
public Response getAllUsers()
{
String result = "<h1>Web Application</h1>In real world application, a collection of users will be returned !!";
return Response.status(200).entity(result).build();
}
}
Deploy the application on tomcat and try to invoke service using following URL:
http://localhost:8088/TestWeb/user-management/users/
Client Code:
Need to download:
jersey-client-1.19.jar
From below location
http://repo1.maven.org/maven2/com/sun/jersey/jersey-archive/1.19/jersey-archive-1.19.zip
Once done then
Create ClientTest class and get the output of the REST result
package com.test.client.service;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class ClientTest
{
public static void main(String[] args)
{
try
{
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8088/TestWeb/user-management/users");
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Client Code:
Need to download:
jersey-client-1.19.jar
From below location
http://repo1.maven.org/maven2/com/sun/jersey/jersey-archive/1.19/jersey-archive-1.19.zip
Once done then
Create ClientTest class and get the output of the REST result
package com.test.client.service;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class ClientTest
{
public static void main(String[] args)
{
try
{
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8088/TestWeb/user-management/users");
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Output:
Output from Server ....
<h1>Web Application</h1>In real world application, a collection of users will be returned !!