为什么在Java中使用RESTful服务的框架而不是vanilla servlet [英] Why use a framework for RESTful services in Java instead of vanilla servlets

查看:80
本文介绍了为什么在Java中使用RESTful服务的框架而不是vanilla servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一些关于可用于在Java中执行RESTful服务的库的问题,但是使用它们对付vanilla实现的价值是什么。我的意思是,如果我想创建 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模式/图像映射到servlet是否容易(对于未来的开发人员)和更快(实现和学习)并且有一行或两行解析参数的url而不是学习,实现和配置其中一个库来为你完成。





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



编辑:这个球衣代码处理得非常整齐,每个人都应该知道怎么做如果他们正在寻找库来为他们做这件事,那就是servlet形式。

  @Path(/ helloworld)
public class HelloWorldResource {

// Java方法将处理HTTP GET请求
@GET
// Java方法将生成由MIME Media $ b $标识的内容b //键入text / plain
@Produces(text / plain)
public String helloWorld(){
//返回一些陈词滥调的文本内容
returnHello世界;
}
}

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

解决方案


(对于未来的开发人员)更快更容易(实现和学习)将url模式 / images 映射到servlet并拥有一行或者两个解析参数的url而不是学习,实现和配置其中一个库来为你做。

...


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



好的,当你只处理简单的webapps时,这些好处可能看起来不那么多。毕竟,你可以在很短的时间内解决问题,并在网上进行调整。然后,你必须祈祷你做对了,没有意外的利用漏洞或拒绝服务攻击的途径。维护程序员在添加小功能或修复错误时,必须了解通过代码喷出的那些正则表达式(Good Luck With That!)。但是随着webapp越来越大,拥有一个经过测试的库来处理所有低级别东西的好处确实很有用。



(在你问之前,一些你提到的库将自己安装为servlet;这允许你的代码只描述servlet的业务逻辑,并声明如何以抽象的方式完成到线的映射。这非常容易。)


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?

解决方案

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

这篇关于为什么在Java中使用RESTful服务的框架而不是vanilla servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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