Servlet vs RESTful
Asked Answered
R

3

54

Today I read about Restful services. Basically what I understand that is Restful webservices will work on HTTP request methods rather than normal webservice will work on SOAP request.

What is the need for Restful services as normal servlet can also work on the HTTP methods?

Ronel answered 24/10, 2011 at 11:10 Comment(0)
G
74

RESTful is more an architecture style than a different technology. In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions). In client perspective, it's more a way of getting information in different formats via URLs with (self-documenting) path parameters instead of request parameters.

Surely you can do this with a plain vanilla servlet, but it would introduce some boilerplate code to gather the path parameters and to generate the desired response. JAX-RS is just a convenient and self-containing API which removes the need for writing all the boilerplate code yourself, resulting in minimal and more self-documenting code.

Assuming that you've a JAXB entity as model as below:

@XmlRootElement
public class Data {

    @XmlElement
    private Long id;

    @XmlElement
    private String value;

    // ...

    @Override
    public String toString() {
        return String.format("Data[id=%d,value=%s]", id, value);
    }

}

And a JAX-RS resource as below:

@Path("data")
public class DataResource {

    @EJB
    private DataService service;

    @GET 
    @Path("text/{id}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getAsText(@PathParam("id") Long id) {
        return String.valueOf(service.find(id));
    }

    @GET 
    @Path("xml/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Data getAsXml(@PathParam("id") Long id) {
        return service.find(id);
    }

    @GET 
    @Path("json/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Data getAsJson(@PathParam("id") Long id) {
        return service.find(id);
    }

}

Then you'd already get the desired content in proper format by:

That's it. Try to do the same with a single plain vanilla Servlet :) Please note that SOAP essentially also goes over HTTP. It's basically an extra XML layer over HTTP, not a different network protocol.

See also:

Gayle answered 24/10, 2011 at 12:47 Comment(6)
Thank you for your reply. So because of coding efficiency only Restful services comes in to play?Ronel
Not exactly that, you can also do the same with plain servlets. The existing JAX-RS API just minimizes the boilerplate. Restful is more about URL formats. It allows you to do actions based on URL paths rather than request parameters.Gayle
@Gayle thank you as always for amazing answers. Could you please help me a little bit by confirming my assumption that if I am building a REST-style application then servlets are obsolete for me? I. e. almost anything I could do with a servlet I could simply do with a POJO + @Resource, @GET, @POST annotations? Is there a scenario where I would use @WebServlet or @WebFilter in my REST-style application?Disannul
@Gayle also a quick clarification, in your example, the DataService with the find method is not a standard class, correct?Disannul
@agks: uh no. It's just your business service class, like as that DataResource is not a "standard class".Gayle
@Gayle thank you for the clarification! re the other question i think servlet abstinence would be a good thing. i am assuming that there is an equivalent of filters in jax-rs and will look out for a tutorial. if there are any stellar answers for that, please let me know! cheers and thanks for amazing answers! :)Disannul
S
2

In my opinion, for a better understanding , we need to dissect components that confuse us and these components are ,

  1. REST Concept

Fielding used REST to design HTTP 1.1 and Uniform Resource Identifiers (URI)

  1. HTTP Protocol - Hypertext Transfer Protocol
  2. javax.servlet.http.HttpServlet class
  3. REST with Java - JAX-RS and its implementations ( like Jersey etc)
  4. Other REST Implementations not conforming to JAX-RS ( like Spring REST ) Difference between JAX-RS and Spring Rest

Then if you refer this answer to understand as how these implementation use a Servlet ( A concrete javax.servlet.http.HttpServlet ) to intercept all incoming requests. Important quote there is,

These REST service classes are simple POJOs annotated to tell the jersey framework about different properties such as path, consumes, produces etc.

Then you can further read about - What is the difference between REST and HTTP protocols? & What is the difference between HTTP and REST? and make a conclusion as what advantages you get if you make your web service RESTFul , namely ( copied from one answer ) ,

REST is not necessarily tied to HTTP. RESTful web services are just web services that follow a RESTful architecture.

What is Rest -
1- Client-server
2- Stateless
3- Cacheable
4- Layered system
5- Code on demand
6- Uniform interface

What is the advantage of using REST instead of non-REST HTTP?

Though, I wouldn't like to get into advantages - disadvantages ( pros & cons ) debate as that is very subjective.

With above readings , now for your question ,

What is the need for Restful services as normal servlet can also work on the HTTP methods?

You would understand that REST Frameworks simply simplify implementation of REST Services at enterprise level but they do use HTTP Servlet to intercept incoming requests. You can always use plain servlets to implement your own REST services but that would simply be more time consuming with lots of boiler plate code.

Schwerin answered 26/9, 2017 at 6:14 Comment(0)
U
-2
  • RESTeasy has a proprietary @Form that binds form parameters (e.g. [BuildVersion]&[BuildVersion]... in the HTTP body) to Java objects. If you are handling form contents, you will save considerable efforts to bind the @formparam to Java objects
  • RESTeasy's proprietary cache for the URL or query

RESTeasy is easier for EJB 3.0 and SEAM integration, whereas Jersey is easier for Spring and JSON integration.

Unrepair answered 12/10, 2020 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.