Servlet与REST [英] Servlet vs REST

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

问题描述

我需要在服务器端创建5个方法,这将使用二进制数据。远程客户端是applet和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服务是一种好习惯吗?或者我应该使用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。而且
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 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. 如果你正在使用一些框架进行休息实现(让我们说 apache cxf jersey )你会得到很多东西的盒子 - 你写POJO你得到休息,你得到序列化和反序列化,让我们说,JSON对象开箱即用(最终你需要实现一些JsonProviders但这不是一个大问题。)

  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进行休息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天全站免登陆