Servlet vs REST

后端 未结 7 1599
野的像风
野的像风 2020-12-08 07:31

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

相关标签:
7条回答
  • 2020-12-08 07:42

    First, you're speaking in terms of two different paradigms. It's kinda apples-and-oranges.

    REST is a style of service that uses HTTP operations (GET, PUT, etc.) to read and write the state of resources. Think of resources as "nouns" and "things".

    Servlet, on the other hand, is a software specification originally provided by Sun Microsystems for connecting HTTP requests to custom Java code. Servlets often speak in terms of method calls: "verbs" and "actions".

    Since your question implies that you are looking to deal with input->output methods, just a plain servlet should do the job.

    0 讨论(0)
  • 2020-12-08 07:47

    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.

    0 讨论(0)
  • 2020-12-08 07:55

    Depending on your container version, Jersey (or any other JAX-RS implementation) will still use a Servlet for dispatching requests to the appropriate handler.

    If your application is truly RESTful, then JAX-RS will do what you want. Otherwise, consider using a FrontController to interpret the Request and forward it to the appropriate handler.

    Also, don't confuse XML or JSON with REST. You will get these for free in most (if not all) JAX-RS implementations, but these implementations still delegate content marshalling to other libraries (e.g. JAXB).

    0 讨论(0)
  • 2020-12-08 07:56

    I cannot see any problems on using Jersey and create a REST service. I know that I'm a REST-taliban but it's really simple to implement this kind of architecture using JAX-RS, so... why not?

    Your colleagues say: "REST must be created only when it will be used by many apps" but I cannot see how this can be true.Why I cannot create a REST service for a single app?

    0 讨论(0)
  • 2020-12-08 07:56

    Here is the similiar link. he has done it with simple servlet http://software.danielwatrous.com/restful-java-servlet/

    0 讨论(0)
  • 2020-12-08 07:57

    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.

    0 讨论(0)
提交回复
热议问题