0

As I understand, REST is a Architectural pattern, where frameworks like Jersey provide out-of-the box methods to specify GET/POST/DELETE etc methods of HTTP. Servlet provides basic functionalities that takes way boilerplate code to handle networking requests.

My question here is that even HttpServlet provides 'do' methods like doGet, doPost and one can also specifiy the path/headers etc. So exposing a webservice via HttpServlet's doGet/doPost adheres to being a RESTful webservice?

user12
  • 55
  • 3
  • 10
  • Yes, it is possible to write a RESTful service using servlets. But it is not enough to use a servlet if you want to be RESTful. –  Jun 02 '16 at 09:30
  • 1
    @LutzHorn What else do you need? – Kayaman Jun 02 '16 at 09:33
  • You'll have to follow the REST principles about resources, usage of HTTP methods, etc. –  Jun 02 '16 at 09:33

1 Answers1

0

While you can implement Rest using Servlets (and there are lots of frameworks available that do it), Rest is an architectural principle that is in no way tied to Servlets or even Java, just as Servlets are an open architecture that let you implement all kinds of things, including Rest. So there is an overlap, but that's it.

For an application to be considered Restful, it needs to implement some or all of the following:

  • semantic Use of HTTP verbs
  • definition of resources (nouns)
  • correct use of HTTP headers and status codes
  • statelessness
  • content negotiation
  • HATEOAS (HyperMedia)

While you can do all of this from scratch using servlets, it's usually a better idea to reuse an existing framework and focus on your own business problems, rather than reinventing wheels.

For more info about Rest, you may refer to this answer to a previous question: https://stackoverflow.com/a/671132/342852

Community
  • 1
  • 1
Sean Patrick Floyd
  • 274,607
  • 58
  • 445
  • 566
  • but doesnt doGet/doPost etc methods of servlets takes care of HTTP verbs, status codes etc? Also for statelessness, @stateless for EJB should do right? I am not sure about content negotiation though. – user12 Jun 02 '16 at 09:51
  • using verbs and using them correctly is not the same thing. everybody can use a paintbrush, but not everybody creates art when using one – Sean Patrick Floyd Jun 02 '16 at 10:00