Archive for the ‘rest’ tag
Accessing RESTful WebServices with Prototype.js
We can make Ajax requests using Prototype.js (version 1.6.0.3) thus:
var url = ...;
new Ajax.Request(
url,
{
method:'DELETE',
onComplete:function doMessage(response){
alert(response.status);
}
});
While working on this code, we found out that the method parameter accepted only GET or POST requests. When we give any other request type like DELETE or PUT, the Prototype.js library converted the request to POST.
Arun Jeganath found this post where the same issue is discussed. The problem was solved when we commented these lines from the Prototype.js code:
if (!['get', 'post'].include(this.method)) {
//simulate other verbs over post
params['_method'] = this.method;
this.method = 'post';
}
Getting started with JBoss RESTEasy 1.0
So ready to jump start into JBoss RESTEasy? Dive!
Writing the JAX-RS Class
The first step, let us write a simple echo service using JAX-RS:
package testpackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/echo")
public class Echo {
@GET
@Path("/{message}")
public Response echoService(@PathParam("message") String message){
return Response.status(200).entity(message).build();
}
}
Adding the JBoss RESTEasy dependency to Maven project
If you are using Maven to build your projects, add the dependency for RESTEasy:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>1.0.0.GA</version>
</dependency>
Also remember to add the JBoss repository to the pom.xml (RESTEasy is not available in Maven public repository):
<repository>
<id>jboss</id>
<url>http://repository.jboss.com/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
Configuring web.xml
The bare minimum needed to have RESTEasy running is:
<web-app ...>
...
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
Above, we have just added the RESTEasy servlet and listener. Now we have to make it load our JAX-RS class. This can be done in two ways. One, just let RESTEasy scan the CLASSPATH to identify and load JAX-RS resources:
<web-app ...>
...
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
...
</web-app>
The other option is to specify the resources manually (use CSV format):
<web-app ...>
...
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>testpackage.Echo</param-value>
</context-param>
...
</web-app>
After deploying this, you may access your application (if your are using default Tomcat/Jetty installation): http://localhost:8080/resteasytest/echo/hello,world (assumes your application is deployed as resteasytest.war)