How to create and consume RESTful webservices
In this blog I will explain how to create and consume
RESTful web service with JAX-RS using Jersey.
Java API for RESTful Web Services (JAX-RS) is a
Java programming language API that provides support in creating web services according
to the Representational State Transfer (REST) architectural pattern. (http://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services
)
In order to simplify development of RESTful Web
services and their clients in Java, a standard and portable JAX-RS API has been designed.
Jersey RESTful Web Services framework is open source, production quality, framework
for developing RESTful Web Services in Java that provides support for JAX-RS
APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation (https://jersey.java.net/).
I have used Eclipse Indigo, JDK 1.7, Jersey1.19
(you can download ‘Jersey 1.19 ZIP bundle’ zip from https://jersey.java.net/download.html
), Apache Tomcat 5.5
Let’s
see step by step to create Restful webservice
SETP1: Create new Dynamic Web Project
STEP2: Add downloaded jersey JARs to project lib
folder. Also, you need to add dependent jars persistence-api-1.0.jar and jersey-container-servlet-core-2.17.jar.
STEP3: Create a RESTful Web Service Resource ( Java class )
package
com.kmingle.webservices.restful;
import javax.ws.rs.GET;
import
javax.ws.rs.Produces;
import
javax.ws.rs.core.MediaType;
import javax.ws.rs.Path;
@Path("/restfulExample")
public class
RestfulServiceProvider {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello(){
return "RESTful Hello
World!";
}
}
Here, @Path("/restfulExample") annotation before class
definition tells that restfulExample REST service can be reached in the
URL .../ restfulExample. @GET annotation before method definition means
that in URL .../ restfulExample, all GET requests are going to be handled by sayHello()
method. You can also pass parameters in a GET request in REST services using URI
path parameter with annotation @Path("/{parameter}") before
method definition.
The @Produces annotation used in the sayHello() method specify
the MIME media types or representations a resource can produce and send back to
the client. If no methods in a resource are able to produce the MIME type in a
client request, the Jersey runtime sends back an HTTP “406 Not Acceptable”
error.
STEP4: Do Servlet mapping in web.xml as mentioned below -
xml version="1.0"
encoding="UTF-8"?>
<web-app id="WebApp_ID"
version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>RestfulWebServiceExample</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>RESTful_Web_Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.kmingle.webservices.restful</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RESTful_Web_Service</servlet-name>
<url-pattern>/restful/*</url-pattern>
</servlet-mapping>
</web-app>
STEP5: Your web service is ready to run start
the server and run web service in browser
URL should be
(http://localhost:8080/) + ( url-pattern in web.xml) + (@Path
annotation value in resource class)
RESTful webservice client( Consumer )
:
To consume
this RESTful webservice we need to create a client. Let’s have a look how to create
a Java Jersey client.
STEP1:
Create new
Java Project
STEP2: Add jersey jars to lib folder, for that
add “Web App Libraries” with RestfulWebserviceExample Project
STEP3: Create client class
package
com.kmingle.webservices.restful;
import
javax.ws.rs.core.MediaType;
import
com.sun.jersey.api.client.Client;
import
com.sun.jersey.api.client.ClientResponse;
import
com.sun.jersey.api.client.WebResource;
public class
RestfulWebserviceClient {
public static void main(String args[]){
try {
Client
client = Client.create();
WebResource
webResource = client.resource("http://localhost:8080/RestfulWebServiceExample/restful/restfulExample");
ClientResponse
response = webResource.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class);
if
(response.getStatus() != 200) {
throw new RuntimeException("HTTP error
code :" + response.getStatus());
}
String
output = response.getEntity(String.class);
System.out.println("Webservice
Response");
System.out.println(output);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Webservice Response
RESTful Hello World!
My next blog will
be on webservice security……
No comments:
Post a Comment