4

Yesterday I had an interview and I faced an interesting question where I got stuck. The Question "How can you say Restful web service is a web service?". I was trying to explain all the possible ways to prove. But all the answers were blocked by the question "Servlets can do the same. So Servlets are restful web service?"

Can anyone share your thoughts?

Ram Kurup
  • 61
  • 1
  • 5
  • possible duplicate of [Servlet vs RESTful](http://stackoverflow.com/questions/7874695/servlet-vs-restful) – jmj Sep 14 '13 at 05:00
  • There's information here that helps explain: http://stackoverflow.com/q/243388/814064 – dcaswell Sep 14 '13 at 05:07

6 Answers6

1

To answer your question, lets ask first what is a web service?

  • In purely abstract terms,

A web service is a method of communication between two electronic devices over the World Wide Web. (Wikipedia)

  • Now the accepted industry norm for the two devices to communicate over the internet is using XML messages (which makes it inter-operable)

  • This brings us to different type of web services, mainly divided into SOAP and RESTful.

  • A SOAP web services use the XML (which conforms the a specific protocol or xml schema which in other words referred as WSDL ). So a SOAP web servies puts certain rules/regulation on the way the messages are exchanged between the web services and their clients. The messages can be exchanged using any convenient protocol apart from HTTP.

  • Now in a RESTful scenario, you still exchange messages (xml/json etc) BUT there are no new additional specifications (I know WADL but its invented more for providing tooling support RESTful and has nothing to do the RESTful web services per se)

  • In RESTful, there are no new protocol definition (for exchanging messages). It uses already established norms of HTTP protocol which are passing parameters in URL as path elements and the HTTP methods to send data (namely GET/POST/PUT/DELETE).

Now coming on to your question of whether Servlets are restful web service are not, lets see what Servlets do

  • Accept the GET/POST request
  • Return an HTML (well generally) (which essentially is XML)

Now if a servlet is written in such a way, that it can be invoked by following URL

http://www.myrestwebservices/services/getstockquote/GOOG

This servlet

  • is mapped to the URL pattern /services/getstockquote

  • gets GOOG as input data in URL path, which it can parse, query some system to get the latest stock quote of Google.

  • Return the data as text/xml to the clients

    Isn't this servlet satisfying the following basic requirements of a RESTful scenario ?

  • Use HTTP methods explicitly

  • Be stateless.

  • Expose directory structure-like URIs.

  • Transfer XML, JavaScript Object Notation (JSON), (text essentially)

So technically speaking, yes a Servlet is a RESTful web services, but that may not be enough for generic business requirement of a web services per se. So for a full blown RESTful web servies, we need a servlet (nevertheless) written specifically to address those basic business requirements.

Community
  • 1
  • 1
Santosh
  • 17,667
  • 4
  • 54
  • 79
0

Well you can possible design RESTful webservices with Servlet.

The Servlet helps you create HTTP response for a HTTP request. RESTful webservices seats on top of HTTP protocol so you can make a REST service with Servlet.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
0

Any code (with any language etc) that is based on HTTP can be a restful web service as long as it conforms to the requirements of REST...

See: http://en.wikipedia.org/wiki/Representational_state_transfer

Menelaos
  • 23,508
  • 18
  • 90
  • 155
0
  1. Everything is a Resource
  2. All resources expose a standard interface GET, POST, PUT, DELETE
  3. REST services are idempotent
  4. Resources may link to other resources
  5. Multiple representations
  6. Stateless communication

See this post for details on the above

cmd
  • 11,622
  • 7
  • 51
  • 61
0

Your approach for such questions should be bottom up. Start with a definition of Service. Then define Web Service and then you can easily distinguish between what is a Web service and what is not. Generally, for such discussion, I attack it like this:

  1. Any reusable piece of code which defines a contract for client is a service. A printer driver on your desktop is a service.
  2. Any Service which can be used over the web ( over HTTP in text request/resposne ) is a Web Service.
  3. A RESTful service adds more contraints in terms of VERB vs NOUN separation and concept of Resources.
  4. How Servlets differ from REST is the absence of contract in Servlets and hence Servlets by themselves are not Services.
  5. Nobody can stop a person who wants to implement RESTful Service using Servlets - but it is too low level for REST development in a world where frameworks exist to help ease development !!

ALL Web Restful services in Java world are written over Servlet which is the lower end implementation for handling HTTP. If the transport is not HTTP, it is a Service but not Web Service :)

Akhilesh Singh
  • 2,548
  • 1
  • 13
  • 10
0

The answer i think is fairly obvious so the question they gave you is a bit peculiar. Both SOAP-based and REST-full services use HTTP as transport mechanism, so in effect they are web services.

Their difference is that SOAP-based services are more strictly defined by a specification whereby REST-full services are more like an architectural style less constrained in their implementation.

Lefteris Laskaridis
  • 2,292
  • 2
  • 24
  • 38