Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet vs REST

I need to create 5 methods on the server side, which will work with binary data. The remote clients are applet and JavaScript. The Client will send files to the server, and the server must parse these files and then return the response as XML/JSON.

So I am confused - is it good practice to use REST-service in this case? Or should I use a servlet?

My colleague told me:

"Creating REST-service that will be used only by one Application isn't good. REST must be created only when it will be used by many apps. And REST has some disadvantages over servlet: REST is slower than servlet; it more difficult to write thread-safe REST than servlet"

However, I see some disadvantages with using Servlet: I need to send a function name that I want to call (i.e. as extra HTTP parameter send function name) and then inside the doPost method perform the following switch:

switch(functionName) {  case "function1":    function1();     break;  case "function2"    function2();     break; //.... more `case` statements....  } 

In case of REST I can simple use different URLs for different functions. Also, in the case of REST, it more convenient to return JSON/XML from server.

like image 917
WelcomeTo Avatar asked Sep 21 '12 07:09

WelcomeTo


People also ask

What is difference between servlet and REST API?

Servlets are API which is simple and provides capabilities to write server side components. Rest provides higher level support for writing services which are denoted by using the concept of resource which represents a concept to be addressed while representation indicates the current state of resource.

Can we create REST API using servlet?

In a similar way, you can create REST applications using only the Servlet API. However, there are other APIs that are designed to create REST applications.

What servlet is called when a REST API request is made?

An HttpServlet is a natural, convenient way to implement RESTful web services for two main reasons.


2 Answers

You are confusing two paradigms here:

  • REST is a software architecture “style”;
  • Servlet is a server-side technology.

You can, for example, implement REST-like services using Servlets.

like image 179
Sébastien Le Callonnec Avatar answered Sep 23 '22 18:09

Sébastien Le Callonnec


Well, I wouldn't agree with your colleagues' opinion that isn't good to have rest used by only one application, since you may decide in the future to have different applications using the same rest api. If I were you I would choose the pure REST. Why?

  1. If you're using some framework for rest implementation (let's say apache cxf or jersey) you get lots of stuff out of the box - you write POJO you get a rest, you get serialization and deserialization, to and from let's say, JSON object out of the box (eventually you will need to implement some JsonProviders but this is not a big deal).

  2. It is intuitive to work (if you design your rest APIs well).

  3. Very easily consumable by JavaScript clients (especially if you're using JQuery or something like that)

However, it strongly depends of what exactly do you want to do, if you have some strong transactional logic, the rest could be quite tricky. If you're only going to do POST requests (without using the other HTTP methods) you might want to use the Servlet since you won't have to work with additional frameworks and making more dependencies. Note that the REST is more or less an architectural concept and it does not contradict with the Servlet technology, if you're stubborn enough you can make a rest api only with servlets :-). Hope I've helped.

like image 45
Cyril Gavrailov Avatar answered Sep 21 '22 18:09

Cyril Gavrailov