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

2022-01-21 00:00:00 rest java jersey

我知道有一些关于可用于在 Java 中执行 RESTful 服务的库的问题,但是将它们用于原始实现的价值是什么.我的意思是,如果我想创建 Wim 描述的 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

将 url 模式/images 映射到 servlet 并使用一两行来解析 url 的参数而不是学习、实现和配置这些库之一来为您完成.

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.

  • Apache CXF
  • 球衣(流行)
  • Restlet(JAX-RS 的先驱)
  • RESTEasy
  • Apache CXF
  • Jersey (popular)
  • Restlet (pioneer of JAX-RS)
  • RESTEasy

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

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?

这个球衣代码处理得非常整齐,如果他们正在寻找库来为他们做这件事,每个人都应该知道如何以 servlet 形式做.

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

如果你要做的只是一个服务",它返回由 URL 参数驱动的文本,所以返回纯文本,是否需要框架?

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?

推荐答案

将 url 模式 /images 映射到 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.

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

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.

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

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.

(在你问之前,你提到的一些库会很高兴地将它们自己安装为 servlet;这允许你的代码只描述 servlet 的业务逻辑并声明如何以抽象的方式完成到线路的映射.这只是巨大的更容易.)

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

相关文章