Using Jersey-RESTful with Codenvy

Instantly create Jersey  project off a Codenvy Factory:

Developing RESTful Web services that seamlessly support exposing your data in a variety of representation media types and abstract away the low-level details of the client-server communication is not an easy task without a good toolkit. 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.
Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides it’s own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs.

Create a Project

Login to your codenvy workspace and create a New WAR Project say “jersey”. Project file structure is shown below :

GetImage (5)

Add Dependencies

Specify the below dependencies in pom.xml which are prerequisites. Make sure these jars are included under Maven Dependencies.

GetImage (6)

Pom.xml :

<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
</dependencies>

Create HelloWorldService Class

In this example, web request from “projectURL/rest/hello/” will match to “HelloWorldService“, via @Path(“/hello”).

And the “{any values}” from “projectURL/rest/hello/{any values}” will match to parameter annotated with @PathParam.

Code Logic :

package com.codenvy;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {

String output = "Hello " + msg;

return Response.status(200).entity(output).build();

}

}

Create index.jsp

Sample index page for the web application giving a sample link (url) to HelloWorldservice created.

<html>
<body>
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="460" bgcolor="#EEFFCA">
<tr><td width="100%"><font size="6" color="#008000"><a href="/rest/hello/codenvy">HelloWorldService</a></font></td></tr>
</table>
</center>
</div>
</body>
</html>

Create web.xml:

<web-app>
<display-name>Restful Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-serlvet</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.codenvy</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

Build and Run the application

Upon successful building the application and navigating to url “projectURL/rest/hello/codenvy”, it displays.

GetImage (7)