Step by Step guide- Hello World REST Service

Post moved to

http://srinichekuri.com/2016/01/17/step-by-step-guide-hello-world-rest-service/

**********************************************************************

This post is a step by step guide for a Hello world REST Service using JAX-RS. I am using Eclipse (Mars Edition) and Apache Tomcat for this tutorial. Also I am using Maven for build automation. If you are beginner or  if you have not yet configured your workspace then I recommend these links before reading any further.

Apache Wink is used for JAX-RS implementation for this tutorial.

Step 1: Create a new Dynamic Web Application

Create a new dynamic web application (named HelloWorldRest for this tutorial). Also convert the project into ‘Maven Project’ (This is an optional step if you are planning to use Maven).

Step 2: Update dependencies for Apache Wink

Add these dependencies to pom.xml

<dependency>
 <groupId>org.apache.wink</groupId>
 <artifactId>wink-server</artifactId>
 <version>1.4</version>
 </dependency>
 <dependency>
 <groupId>org.apache.wink</groupId>
 <artifactId>wink-common</artifactId>
 <version>1.4</version>
</dependency>

If you are not using Maven then download the following jar version into WEB-INF/lib folder.

activation.jar -> 1.1 Version
commons-lang.jar -> 2.3 Version
geronimo annotation_1.1_spec.jar -> 1.0 Version
geronimo-jaxrs_1.1_spec.jar -> 1.0 Version
jaxb-api.jar -> 2.2 Version
jaxb-impl.jar -> 2.2.1.1 Version
slf4j-api.jar -> 1.6.1 Version
stax-api.jar -> 1.0-2 Version
wink-common.jar -> 1.4 Version
wink-server.jar -> 1.4 Version

Step -3: Add code for REST Service

Add below code for HelloWorldResource

 

package com.test.helloworld.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path(&quot;/helloworld&quot;)
public class HelloWorldResource {

	@GET
	 public String getMessage() {
		System.out.println(&quot;Returning Message&quot;);
		return &quot;Hello World!&quot;;
	 }
}


Add below code for HelloWorldApplication

 

package com.test.helloworld;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import com.test.helloworld.resource.HelloWorldResource;

public class HelloWorldApplication extends Application{

	@Override
	 public Set&lt;Class&lt;?&gt;&gt; getClasses() {
		 Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;Class&lt;?&gt;&gt;();
		 classes.add(HelloWorldResource.class);
		 return classes;
	}
}


Add below entries to web.xml

<servlet>
 <servlet-name>HelloWorldApp</servlet-name>
 <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
 <init-param>
  <param-name>javax.ws.rs.Application</param-name>
  <param-value>com.test.helloworld.HelloWorldApplication</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>HelloWorldApp</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Step-4: Test HelloWorld REST Service

helloworld_rest_service