为什么使用 Java 中的 RESTful 服务框架而不是普通 servlet

发布于 2024-12-28 04:14:16 字数 1404 浏览 0 评论 0原文

我知道关于可用于在 Java 中执行 RESTful 服务的库存在一些问题,但是在普通实现中使用它们有什么价值。我的意思是,如果我想创建 Wim 描述的 url 结构

  • www.example.com/images
  • www.example.com/images/id/num
  • www.example.com/images/tag/num
  • www.example.com/images/tag/num/num/num

这不是更容易(对于未来的开发人员)和更快(实施和学习)将 url 模式 /images 映射到 servlet,并使用一两行代码来解析参数的 url,而不是学习、实现和配置这些库之一来为您完成此操作。

本质上我要问的是......使用 RESTful Java 框架的价值是什么?对于一个简单的问题,在实现过程中不会增加很多复杂性吗?

编辑:这个球衣代码处理得非常巧妙,如果每个人正在寻找库来为他们做这件事,他们都应该知道如何以 servlet 形式完成它。

@Path("/helloworld")
public class HelloWorldResource {

    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String helloWorld() {
        // Return some cliched textual content
        return "Hello World";
    }
}

如果您要做的只是一个返回由 URL 参数驱动的文本的“服务”,因此返回纯文本,那么是否需要一个框架?

I know there are a few questions regarding the libraries you can use to do RESTful services in Java, but what is the value in using them against vanilla implementations. I mean, if i was looking to create the url structure described by Wim

  • www.example.com/images
  • www.example.com/images/id/num
  • www.example.com/images/tag/num
  • www.example.com/images/tag/num/num/num

Would it not be easier (for future developers) and faster (to implement and learn) to map the url pattern /images to a servlet and have a line or two that parses the url for the parameters instead of learning, implementing and configuring one of these libraries to do it for you.

Essentially what I am asking is... What is the value in using a RESTful Java framework? Would it not be adding a lot of complexity, in the implementation, for a simple problem?

EDIT: This jersey code is handled very neatly and everyone should know how to do it in servlet form if they are looking into libraries to do it for them.

@Path("/helloworld")
public class HelloWorldResource {

    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String helloWorld() {
        // Return some cliched textual content
        return "Hello World";
    }
}

If all you are going to be doing is a "service" that returns text that is driven by URL parameters, so plain text returns, is a framework necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路终见情 2025-01-04 04:14:16

将 url 模式 /images 映射到 servlet 并用一两行代码来解析 url,这不是更容易(对于未来的开发人员)、更快(实现和学习)吗参数,而不是学习、实现和配置这些库之一来为您完成此操作。
...

更轻松?编写起来肯定不容易——您必须自己完成所有路径提取、所有方法处理和所有内容类型协商(两个方向)以及所有 cookie 处理和对象反序列化/serialization thunks 以及……好吧,很多低级的东西都需要测试和调试——或者更容易维护,因为 JAX-RS 接口允许您在资源级别进行操作(RESTful web 应用程序的自然特征)而不是请求;凭借丰富的经验,当概念模型和实现之间的差距最小时,维护是最容易的。它的实现速度也不是更快(因为 JAX-RS 的低级实现已经为您进行了测试和调试;您需要做的事情更少)并且学习它的成本不是很高,因为它主要是声明性 API很少有惊喜。

好吧,当您只处理简单的网络应用程序时,这些好处可能看起来并不那么明显。毕竟,你可以在很短的时间内破解一些东西,并将所得的应急结果放到网上。然后,您将不得不祈祷您的做法正确,不会出现重大的意外漏洞或拒绝服务攻击。当添加小功能或修复错误时,维护程序员必须了解您在代码中喷洒的那些正则表达式的作用(祝你好运!)。但随着 Web 应用程序变得越来越大,拥有一个经过测试的库来处理所有低级内容的好处确实会胜出。

(在您提出问题之前,您提到的一些库会愉快地将自身安装为 servlet;这允许您的代码仅描述 servlet 的业务逻辑并声明如何以抽象术语完成到线路的映射。这非常容易。)

Would it not be easier (for future developers) and faster (to implement and learn) to map the url pattern /images to a servlet and have a line or two that parses the url for the parameters instead of learning, implementing and configuring one of these libraries to do it for you.

Easier? It's certainly not easier to write — you've got to do all the path extraction yourself and all the method handling and all the content type negotiation (in both directions) and all the cookie handling and the object deserialization/serialization thunks and … well, lots of low-level stuff that would all need testing and debugging — or easier to maintain either, since the JAX-RS interface lets you operate at the level of resources (the natural characterization of RESTful webapps) instead of requests; with much experience, maintenance is easiest when the gap between conceptual model and implementation is smallest. It's also not faster to implement (because the low-level implementations of JAX-RS have already been tested and debugged for you; less for you to do) and the cost of learning it isn't very high as it is a mostly declarative API with very few surprises.

OK, these benefits might not seem so much when you're only dealing with simple webapps. After all, you can hack something out in very little time and put the resulting lash-up online. You'll then have to pray that you got it right without significant unexpected avenues for exploits or denial-of-service attacks. And the maintenance programmers will have to understand just what those regular expressions you've sprayed through the code do (Good Luck With That!) when adding small features or fixing bugs. But as the webapp gets larger, the benefit of having a tested library to handle all the low-level stuff really does win out.

(Before you ask, some of the libraries you mention will merrily install themselves as servlets; this allows your code to just describe the business logic of the servlet and declare how mapping to the wire is done in abstract terms. That's just enormously easier.)

山田美奈子 2025-01-04 04:14:16

JAX-RS 是一个设计精良的 API,它使得将 HTTP 请求映射到方法、从 HTTP 请求的各个部分提取参数、处理内容协商以及许多其他低级任务变得非常容易。

使用 JAX-RS(主要通过 Apache CXF)大约两年了,我总是更喜欢它而不是普通的 Servlet。

JAX-RS is a very well designed API that makes mapping HTTP requests to methods, extracting parameters from various parts of a HTTP request, handling content negotiating, and many other low level tasks very easy.

Using JAX-RS, mainly via Apache CXF, for roughly two years now, I'd always prefer it over plain Servlets.

笨笨の傻瓜 2025-01-04 04:14:16

框架用于使您的任务更加轻松。我同意我们可以通过实现 servlet 来完成同样的事情,然后解析 url,然后实现基本逻辑。

但是,如果您使用像 jersey 这样的框架,那么您不必担心那些解析模式和其他类似的任务。 ServletContainer 类将处理这个问题(在其服务方法中解析 url),并且还有许多其他类,这将使您的任务变得更容易。

还有一件事,我们只采用一种场景(匹配模式),但是当我们的需求增长时,我们自己的 servlet 编写的相同代码将变得更加复杂。

Frameworks are used to make you task more easier. i agree that we can do the same thing by implementing servlets and then parse the url and then implement the basic logic.

Bu if you are using framework like jersey then u don't have to worry about those parsing patterns and other similar tasks. ServletContainer class will take care of this (parsing url in it's service method) and there are lots of other classes also which will make your task easier.

And one more thing we are taking only a one scenario (matching patterns) but when our requirements will grow the same code written by our own servlets will become more complicated and complex.

如歌彻婉言 2025-01-04 04:14:16

在这种情况下,我会选择使用 Jersey 或库,如果它执行以下操作(添加足够的值):

  • URL 的配置全部独立于一个文件中,源代码中
  • 没有隐藏的配置文件或过于冗长配置(例如 web.xml)
  • 参数很好地映射到强类型变量(例如使用注释),
  • 获取参数值并不复杂(例如 RESTlet
  • 运行该库的开销很低(我在其他库中使用反射解决方案时有过不好的经历)
  • 有很好的文档记录
  • 它 在这种

情况下,我发现使用库会为我的项目增加使用它所需的努力的价值。尽管我还没有对其他框架进行足够的调查,但泽西岛似乎确实相当充分地满足了要求。

I would go with using Jersey or a library in this case, if it does the following (adds enough value):

  • the config for the URL is all self-contained within one file, with the source
  • there are no hidden config files or overly verbose configs (e.g. web.xml)
  • the parameters are nicely mapped to strongly typed variables (e.g. use of annotations)
  • it is not convoluted to get to the values of parameters (e.g. RESTlet)
  • the overhead for running the library is low (I have had bad experiences with reflection solutions in other libraries)
  • it is well documented
  • it is well adopted

In such a scenario, I find that using a library would add value to my project for the effort required to use it. Jersey does seem to mach the requirements quite adequately, although I have not given other frameworks enough investigation yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文