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.
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.
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.
An HttpServlet is a natural, convenient way to implement RESTful web services for two main reasons.
You are confusing two paradigms here:
You can, for example, implement REST-like services using Servlets.
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?
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).
It is intuitive to work (if you design your rest APIs well).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With