Servlet 与 REST [英] Servlet vs REST

查看:20
本文介绍了Servlet 与 REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在服务器端创建 5 个方法,它们可以处理二进制数据.远程客户端是小程序和 JavaScript.客户端将文件发送到服务器,服务器必须解析这些文件,然后以 XML/JSON 格式返回响应.

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.

所以我很困惑 - 在这种情况下使用 REST-service 是一种好习惯吗?还是应该使用 servlet?

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

我的同事告诉我:

创建仅由一个应用程序使用的 REST 服务不是好的.只有当它被许多应用程序使用时,才必须创建 REST.和REST 与 servlet 相比有一些缺点: REST 比 servlet 慢;编写线程安全的 REST 比 servlet 更难"

"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"

但是,我发现使用 Servlet 有一些缺点:我需要发送一个我想调用的函数名(即作为额外的 HTTP 参数发送函数名)然后在 doPost 方法中执行以下切换:

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....

}

在 REST 的情况下,我可以简单地为不同的功能使用不同的 URL.此外,在 REST 的情况下,从服务器返回 JSON/XML 更方便.

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.

推荐答案

嗯,我不同意你同事的意见,即让 rest 仅由一个应用程序使用是不好的,因为你将来可能决定让不同的应用程序使用相同的 rest api.如果我是你,我会选择纯粹的 REST.为什么?

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. 如果你正在使用一些框架来实现 REST(比如 apache cxfjersey) 你得到了很多开箱即用的东西——你写了 POJO 你得到了休息,你得到了序列化和反序列化,往返比方说,开箱即用的 JSON 对象(最终您将需要实现一些 JsonProvider,但这不是什么大问题).

  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).

工作起来很直观(如果你设计了你的休息 API).

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

JavaScript 客户端很容易使用(特别是如果您使用 JQuery 或类似的东西)

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

然而,这在很大程度上取决于您究竟想做什么,如果您有一些强大的事务逻辑,其余的可能会非常棘手.如果您只打算执行 POST 请求(不使用其他 HTTP 方法),您可能想要使用 Servlet,因为您不必使用其他框架并创建更多依赖项.请注意,REST 或多或少是一个架构概念,它与 Servlet 技术并不矛盾,如果您足够顽固,您可以仅使用 servlet 制作 REST api :-).希望我有所帮助.

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.

这篇关于Servlet 与 REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆