In this Article, I will show How to create, build, deploy and test RestFul Web Service.
Tools and technologies Uses :
1) jersey-bom – 2.26-b01
2) jersey-container-servlet-core
3) eclipse version Luna 4.4.1.
4) Maven 4.0.0
5) apache-tomcat-8.0.22
6) JDK 1.6 or above
Steps to be followed :
1) Create Maven RESTFul Project in Eclipse.
2) Build the web project RESTFullWSQuickStart.
3) Deploy the project RESTFullWSQuickStart in to server (tomcat).
4) Demo
1) Create Maven RESTFul Project in Eclipse:
In eclipse, Go to File->New->Other and Select Maven->Maven Project.
The project creation wizard will open up. Select Jersey provided archetype ‘jersey-quickstart-webapp’
with groupId ‘org.glassfish.jersey.archetypes’.
Click on next button, In next window provide the groupId as “com.devjavasource.restfull”
and artifactId as “RESTFullWSQuickStart”.
And click on Finish, then maven project will be created.
In this application, we have total number of three resources pom.xml, MyResource.java and web.xml file.
pom.xml
This is the maven file,
<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>com.devjavasource.restfull</groupId> <artifactId>RESTFullWSQuickStart</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RESTFullWSQuickStart</name> <build> <finalName>RESTFullWSQuickStart</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <inherited>true</inherited> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <!-- use the following artifactId if you don't need servlet 2.x compatibility --> <!-- artifactId>jersey-container-servlet</artifactId --> </dependency> <!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> </dependency> --> </dependencies> <properties> <jersey.version>2.26-b01</jersey.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
MyResource.java
Here MyResource is a POJO class, In order to make this as service.
We need to add three annotations @Path, @Get and @Produces.
1) @Path – Represents the web application path, In our case it is “myresource”.
The value of the annotation is the URI relative to the web application context, as deployed in the web server.
In our example MyResource.getIt() will act as a RESTFul service.
2) @Get – Web request type Get.
3) @Produces – Response content type.
package com.devjavasource.restfull.RESTFullWSQuickStart; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; /** * Root resource (exposed at "myresource" path) */ @Path("myresource") public class MyResource { /** * Method handling HTTP GET requests. The returned object will be sent * to the client as "text/plain" media type. * * @return String that will be returned as a text/plain response. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Response is from - RESTFull Webserverces Quick Start..."; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- This web.xml file is not required when using Servlet 3.0 container, see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>Jersey Web 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.devjavasource.restfull.RESTFullWSQuickStart</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/webapi/*</url-pattern> </servlet-mapping> </web-app>
index.html
<html> <body> <h2>RESTfull Web Service Quick Start!!!!</h2> <p><a href="webapi/myresource">Click Me To get Response</a> <p>Visit <a href="http://jersey.java.net">Project Jersey website</a> for more information on Jersey! </body> </html>
2) Build the web project RESTFullWSQuickStart:
This is same as how we are building maven project, right click on pom.xml file and click on “maven instal”.
Once the project is build Successfully, Then RESTFullWSQuickStart.war file will be created.
3) Deploy the project RESTFullWSQuickStart in to server (tomcat):
Add RESTFullWSQuickStart.war to the server and start the server.
4) Demo:
Go to browser and provide the URL “http://localhost:8080/RESTFullWSQuickStart/”
Click on the link “Click Me To get Response” to get the response from service “MyResource.java” class.
You can download complete project, Here
*** Venkat – Happy learning ****