Tuesday 14 May 2013

Deploying Restful Web Services (JAX-RS)


Hi Friends...
I would like to share you some information regarding deploying Restful Webserives.

Here to implement JAX-RS am using Jersey, which is well matured and production quality implementated api.

Deploying Jersey Restful service can be achieved in following ways:

Method1:
JAX-RS provides abstract class Application for declaring root resource classes.
Override getClasses method with all the resource classes which you want to provide as a service.

The code for providing access for the resources is:

package app;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> set = new HashSet<Class<?>>();
set.add(com.navin.first.Hello.class);
set.add(com.navin.model.TodoResource.class);
return set;
    }
}

Check web.xml configuration after Method2.

Method2:
Another alternative way is by extending PackagesResourceConfig class.
Here the classes provide in this implementation are automatically added to the set of classes that are returned by getClasses in above method (Method1).

package app;
import com.sun.jersey.api.core.PackagesResourceConfig;

public class MyApplication extends PackagesResourceConfig {
    public MyApplication2(){
super("com.navin.first;com.navin.model");
    }
}


web.xml configurations for above two methods:
We should configure Jersey specific servlet for loading our resources.
Snippet for Jersey specific servlet is given below:

<servlet>
  <servlet-name>Jersey REST Service</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>app.MyApplication</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>


Method3:
This is the simplest approach to let Jersey choose the PackagesResourceConfig implementation automatically by declaring the packages as follows:

<servlet>
  <servlet-name>Jersey REST Service</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.navin.first;com.navin.model</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

By doing this you are ready with your settings for deploying your JAX-RS Restful application.

Further, its very easy to develop your Restful Web services applications.

Happy coding... :)

1 comment:


  1. yes good description about object,exceptions and errors it will be remember to me,but i want more information from this topic
    Back to original

    ReplyDelete