在 Java 中将 URL 映射到请求处理程序的最简单方法
我正在用 Java 为内部应用程序构建一个简单的 RPC 服务器原型。
虽然最终我想切换到专门的高性能 RPC 库,例如 MessagePack、Apache Avro 或与 0mq 的 Java 绑定,但现在我想让事情尽可能简单,所以我从 JSON 开始HTTP。我想使用 Jetty,这样我就可以嵌入网络服务器并使安装和部署尽可能简单。但我在 Java webdev 方面没有太多经验,并且对将少数特定请求处理程序对象中的每一个与特定 URL 相关联这一非常简单的挑战感到困惑。我的总体印象是,这可能是 web.xml 文件的用途,但如果可能的话,我宁愿将其连接到源代码中。
我应该强调的是,我不需要有趣的 Web 应用程序功能,例如会话、模板、数据库访问等。我只需要从 POST 请求正文中获取 JSON 字符串,执行操作,然后在响应中写回一个字符串。
所以基本上,编写两个请求处理程序(servlet?)的最佳方法是什么,它们分别返回大写和小写的请求正文,并将它们注册到我的服务器上的路径“/upper”和“/lower”,最好不使用配置文件。这将在嵌入式 Jetty 应用程序中进行,但如果您不想的话,当然不需要依赖它。
I'm prototyping a simple RPC server in Java for an internal application.
While eventually I'd like to switch to a specialized, high performance RPC library such as MessagePack, Apache Avro, or the Java bindings to 0mq, right now I'd like to keep things as simple as possible, so am starting with JSON over HTTP. I'd like to use Jetty so that I can embed the webserver and make installation and deployment as easy as possible. But I don't have much experience with Java webdev, and am getting hung up on the very simple challenge of associating each of a handful of specific request handler objects with a specific URL. My general impression is that this may be the kind of thing the web.xml file is used for, but I'd rather wire it into the source code if possible.
I should emphasize that I need no interesting webapp functionality like sessions, templating, database access, etc. I just need to get a JSON string out of a POST request body, do stuff, and then write a string back in the response.
So basically, what's the best way to, say, write two request handlers (servlets?), which return the request body uppercased and lowercased, respectively, and register them with the paths "/upper" and "/lower" on my server, preferably without using a config file. This will be taking place in an embedded Jetty app, but you certainly don't need to depend on that if you don't want to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我会用 Spring 来做。 Spring 可以做很多事情,正如您所说,您不需要大量的 Web 功能,但您仍然可以从中挑选和选择您想要的功能。
根据您的需要,您可以使用单个控制器设置 Spring,并在处理请求的方法上使用 @ResponseBody 注释。然后,您告诉 Spring 将响应对象序列化为 JSON(我认为这实际上是默认值)。然后 Spring 为您处理序列化,您的代码只处理 Java 对象。
这是关于我正在讨论的功能的更详细的文章: http ://java.dzone.com/articles/spring-3-rest-json-path-variables
如果你四处搜索,你可能会看到很多带有一堆令人讨厌的 xml 配置的示例,但是不要不用担心,Spring 的更高版本只需几行Java代码即可配置。
就您有关使用 Avro 的说明而言,设置它实际上并不像您想象的那么困难。以下是在服务器组件的 servlet 容器内使用 Avro 的完整示例应用程序:
http://code.google.com/p/avro-http-example /wiki/指南
Personally I'd do it with Spring. Spring can do a lot and as you've said, you don't need a ton of web functionality, but you can still pick and choose functionality that you want from it.
For what you need, you would set up Spring with a single controller and use a @ResponseBody annotation on the method handling the request. You then tell Spring to serialize response objects to JSON (I think it's the default actually). Then Spring handles the serialization for you and your code just deals with Java objects.
Here's a more detailed article on the functionality I'm talking about: http://java.dzone.com/articles/spring-3-rest-json-path-variables
if you go searching around you'll likely see A lot of examples with a bunch of nasty xml configuration, but don't worry, later versions of Spring can be configured with just a few lines of Java code.
To address your note about using Avro, it actually not not be as tough as you might think to get it set up. Here's a full sample application using Avro inside a servlet container for your server component:
http://code.google.com/p/avro-http-example/wiki/Guide
如果您不想设置配置文件,Jetty API 对您来说应该非常方便。从此处开始。
If you don't want to set up a config file, the Jetty API should be pretty handy for you. Start here.