The Dark Knight
The Dark Knight

Reputation: 5585

Why use Restful Webservices when the same thing can be done by servlets?

I am a complete novice in restful web services . So please bear with me. I am doing a small POC on restful WS . I have previously worked in SOAP based service(Jax-RPC). The immediate difference i found was that there is no WSDL here . The URL that's hosted by the service provider acts as the end point . We normally use jackson or jersey for json or xml based request parameters and have annotations like @get, @post etc before a method which intercepts the request and we can get the parameters from the hosted service and use them for our benefits .

Now, it seems to me that we can do the same in a normal servlet . We can hit the url and get the request parameters and values from it using : request.getParameter("param") in side a doPost() method. Why do we need restful way of getting the parameters here ?

So the question boils down to : when do we use restful web services and when do we use just plain, simple servlets in such scenarios ?

Forgive me for being blunt with the question, I am just trying to understand more .

Upvotes: 2

Views: 247

Answers (3)

Drejc
Drejc

Reputation: 14286

In case you have only one end point there is probably no benefit to do it with RestEasy or some other framework.

But in the real world those REST end points pile up and frameworks can help keeping things under control. Plus they provide additional help with type checking etc.

So take this example for instance:

@GET
@Path("/app/{applicationId}")
@Produces(MediaType.APPLICATION_JSON)
public AppJSON updateStoreData(@PathParam("applicationId") Long appId, @QueryParam("platform") Platform platform) throws RestException {

        AppJSON application = applications.getApp(appId, platform);
        return application;
    }

It describes a GET method ending on /app/applicationId where applicationId should be a Long value. Plus it has a query string parameter of type Platform. Those feed into a service method which in turn returns some object. The output is then converted to JSON format.

A lot of things are taken care of by the framework:

  1. mapping the correct URL to your call
  2. path and query parameters are parsed automatically
  3. type checking and conversion of parameters
  4. conversion of return values
  5. exception management (in case you throw one)

And there is more (like Filtering and Response conversion ...)

Upvotes: 1

Simon Zambrovski
Simon Zambrovski

Reputation: 756

Restful WS has more convenient method for producing response and for listening to path URL. So using The annotations you can create much more elaborate resource URL structures. Using servlets you had to parse the query string on your own. The supported return types are another convenient speedup.

Another issue is, that JAX-RS also runs on top of a usual CDI component or EJB. You just annotate the business method with JAX-RS annotations. Writing servlets means to be locked into implementing the Servlets API.

Upvotes: 1

Will Hartung
Will Hartung

Reputation: 118631

A servlet can work just fine. The benefit of using framework for web services is the same value as using a framework for websites. They do some of the mundane lifting. JAX-RS for example offers easier URL routing, and simple binding from/to XML/JSON and Java beans.

You get that "for free", among other things.

So, the frameworks simply can make development easier, but they're certainly not required.

Upvotes: 4

Related Questions