6

I am building servlets which implement a RESTful API. I understand the Jersey is a framework for deciphering and using given URL's. How do I use it in conjunction with the HttpServlet class.

I don't understand how the two work with each other. I guess this is a very broadstrokes question but I have done a fair share of reading but am still stuck on this seemingly trivial concept. I have attempted to deploy apps with classes that extend the HttpServletclass AND use Jersey annotations.

@Path("/api")
public class API extends HttpServlet{

@GET
@Path("/{name}")
@Produces("text/hmtl")
public String doGetSayHello(@PathParam("name") String name){
    return "Hello" + name;
}
@GET
@Path("/articles")
@Produces("text/json")
public String doGetArticles(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

    JSONObject obj = new JSONObject();
    obj.put("interns", interns);
    obj.put("company", "Stack Overflow");

    return obj.toString();

}

}

Any help or informative materials would be greatly appreciated!

sahibeast
  • 341
  • 3
  • 13

2 Answers2

13

Actually you are confused because you don't understand how jersey works. Jersey framework basically uses com.sun.jersey.spi.container.servlet.ServletContainer servlet to intercept all the incoming requests. As we configure in our projects web.xml, that all the incoming rest request should be handled by that servlet. There is an init-param that is configured with the jersey servlet to find your REST service classes. REST service classes are not Servlet and they need NOT to extend the HttpServlet as you did in your code. These REST service classes are simple POJOs annotated to tell the jersey framework about different properties such as path, consumes, produces etc. When you return from your service method, jersey takes care of marshalling those objects in the defined 'PRODUCES' responseType and write it on the client stream. Here is a sample of jersey config in web.xml

<servlet>
        <servlet-name>REST</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.rest.services;
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>REST</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • What do you mean by REST service classes are not Servlet? – sahibeast Jul 18 '13 at 06:05
  • 1
    While using jersey, you write simple java classes and not servlets, Those classes just need to be annotated for the jersey framework to expose the methods in your class as REST services. – Juned Ahsan Jul 18 '13 at 06:09
5

Jersey uses a servlet to route URLs to the appropriate service. Your service itself does not need to extend a servlet.

At a high level, Jersey's ServletContainer class accepts the requests, and then based on your Jersey configuration, your web service will be invoked. You configure what url patterns are processed by Jersey. Check out section 5.3 http://www.vogella.com/articles/REST/.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • So then what exactly is the purpose of the HttpServlet class? – sahibeast Jul 18 '13 at 06:09
  • The servlet class is the one that handles the web request. In the case of Jersey, they wrote a class ServletContainer that extends HttpServlet. Its implementation is what invokes the appropriate web service. You can write your own servlets if you want to handle the http request directly (http://www.mkyong.com/servlet/a-simple-servlet-example-write-deploy-run/). A lot of web frameworks (including Jersey) use what is called the Front Controller pattern - it's a servlet that incoming requests and routes them to the appropriate place. Just less boilerplate code you have to write. – Jeff Storey Jul 18 '13 at 13:54
  • ahhh ok thanks so much! This really clears things up. And all this time I thought I was trying to write my own servlet somehow trying to integrate Jersey with it. – sahibeast Jul 18 '13 at 18:51