Google

Apr 1, 2014

RESTEasy web service tutorial basic

Step 1: Create a maven project structure with the following archetype maven command. This assumes that Java and Maven are set up as per the previous tutorials.



mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simpleRestWeb -DarchetypeArtifactId=maven-archetype-webapp

Step 2: The above command creates a basic Java web structure.



Step 3: Import this into eclipse IDE. File --> Import



Step 4: Click "Next" and browse the folder simpleRestWeb you just created via mvn archetype plugin.


Step 5: Create the "java" source folder by right mouse clicking on simpleRestWeb folder, and then "Build Path" --> "New Souse Folder". type "src/main/java" as the folder name.

Step 6: Update the pom.xml file with the RESEasy library dependencies.


<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.mytutorial</groupId>
 <artifactId>simpleRestWeb</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>simpleRestWeb Maven Webapp</name>
 <url>http://maven.apache.org</url>
 
 <properties>
  <resteasy.version>2.3.6.Final</resteasy.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>

  <!-- JAX-RS -->
  
  <dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>jaxrs-api</artifactId>
   <!-- <scope>provided</scope> -->
   <version>${resteasy.version}</version>
  </dependency>
  <dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-jaxrs</artifactId>
   <!-- <scope>provided</scope> -->
   <version>${resteasy.version}</version>
  </dependency>
  <dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-jaxb-provider</artifactId>
   <!-- <scope>provided</scope> -->
   <version>${resteasy.version}</version>
  </dependency>

 </dependencies>
 <build>
  <finalName>simpleRestWeb</finalName>
 </build>
</project>


Step 7: Define web.xml file

<!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>Archetype Created Web Application</display-name>

 <context-param>
  <param-name>resteasy.scan</param-name>
  <param-value>true</param-value>
 </context-param>

 <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-simple</servlet-name>
  <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
 </servlet>


 <servlet-mapping>
  <servlet-name>resteasy-simple</servlet-name>
  <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>


This indicates that relative path will be something like /rest/*

Step 8: If you are deploying to a jboss container, define  jboss-web.xml. Otherwise the context root will be simpleRestWeb.

<jboss-web>
   <context-root>tutorial</context-root>
</jboss-web>

Step 9: Define a simple RESTful web  service interface.

package com.mytutorial;

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

@Path("/myapp")
public interface SimpleRestWeb {

 @GET
 @Path("/name/{name}")
 public String sayHello(@PathParam("name") String name);

}

Step 10: The web service implementation

package com.mytutorial;

public class SimpleRestWebImpl implements SimpleRestWeb {

 @Override
 public String sayHello(String name) {
  String result = "Hello " + name;
  return result;
 }

}

Step 11:  Execute mvn clean install from a DOS prompt to build the war file.

Step 12: Deploy the war file to JBoss or any other server.

Step 13:  The url to test

JBoss server with jboss-web.xml

http://localhost:8080/tutorial/rest/myapp/name/arul

The output is: Hello arul

If deployed to any other application server without the  jboss-web.xml

http://localhost:8080/simpleRestWeb/rest/myapp/name/arul






Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home