Using RESTEasy with Codenvy

Instantly create a RestEasy project off a Codenvy Factory:



RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a fully certified and portable implementation of the JAX-RS specification. JAX-RS is a new JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol.

Create a Project

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

GetImage (13)

Add Dependencies

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

GetImage (14)

Pom.xml :

<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.Final</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 printMessage(@PathParam("param") String msg) {
String result = "RESTEasy says : Hello " + msg;
return Response.status(200).entity(result).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:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Restful Web Application</display-name>
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</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 (15)