如何使用 JAXRS 和 JAXB 设置 Restlet 服务器?

发布于 2024-11-01 06:41:11 字数 227 浏览 11 评论 0原文

我一直在互联网上查找,试图找到如何执行此操作的示例。我只是想设置一个 REST 服务器,它自动将对象序列化到 XML 或从 XML 序列化对象。我只是想提供一个服务器,以便于用户登录、注销以及仅在用户登录后访问 XML 对象列表。启动并运行一个简单的示例应用程序需要什么?

我无法掌握 Restlet 库的工作方式,而且我对使用 JAXB 和 JAXRS 完全陌生。我曾经参与过一个使用这些库的项目,但只是从客户的角度来看。

I've been looking all over the internet trying to find an example of how to do this. I simply want to set up a REST server which automatically serializes objects to and from XML. I'm simply trying to provide a server that can faciltate user login, logout, and access to a XML list of objects only once a user is logged in. What's required to get a simple example application up and going?

I'm failing to grasp the way that the Restlet library works, and I'm entirely new to using JAXB and JAXRS. I've worked on a project that uses these libraries, but only from the client's perspective.

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

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

发布评论

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

评论(3

痴者 2024-11-08 06:41:11

Restlet可以看作是JAXRS的实现。地址提供了一个简单的教程:
http://wiki.restlet.org/docs_1 .1/13-restlet/28-restlet/57-restlet.html

您需要将以下 jar 文件放入类路径中。这些 jar 文件可以在 Restlet 发行版中找到(例如版本 2.0.5):

  • javax.ws.rs.jar
  • org.restlet.ext.jaxrs.jar
  • org.restlet.jar

正如您在教程中看到的,您实现您的 JAXRS 资源和应用程序。然后有两种不同的方法来启动整个 Web 应用程序:

  • 使用内置的 Restlet 服务器
  • 使用 JavaEE Web 容器

为了集成 JAXB 支持,您首先需要了解 Restlet 的表示支持如何工作。 REST 请求/响应的内容包含在表示中。支持不同的格式,这是开放和可扩展的。此表示支持可与转换器实体和转换器服务一起使用。

转换器实体负责将一个元素转换为另一个元素。例如,如果您传递一个 Java 实例,并且希望将其转换为 XML 以在 REST 响应中发回。转换器服务负责根据媒体类型和支持内容协商(Accept 和 Content-Type 标头中定义的内容类型)以智能方式处理此转换。当转换器出现在类路径中时,会自动注册。

对于 JAXB,让我们将以下 jar 放入您的类路径中:

  • activation.jar
  • jaxb-api.jar
  • jsr173_1.0_api.jar

让我们举个例子:

  • 您在 Restlet JAXRS 应用程序上发送 REST 请求。您使用值 application/xml 指定 Accept 标头,因为您期望响应内容为 XML 内容。

  • 在您的资源中,请求和所需内容类型的相应 JAXRS 方法会返回一个对象。 Restlet 会自动检查是否有已注册的转换器来处理 Java 对象到 XML 之间的转换。如果您添加了 Restlet JAXB 扩展,那么当 Java 对象使用 JAXB 注释进行注释时,它将使用它。

  • JAXB 转换器将使用 JAXB 转换器来生成 XML 输出。

  • 响应被发送回客户端。

否则,您想要实施什么样的安全性?基于用户/密码?

希望它很清楚并且会有帮助!
蒂埃里

Restlet can be seen as a JAXRS implementation. A simple tutorial is provided at address:
http://wiki.restlet.org/docs_1.1/13-restlet/28-restlet/57-restlet.html

You need to put following jar files in your classpath. These jar files can be found in the restlet distribution (version 2.0.5 for example):

  • javax.ws.rs.jar
  • org.restlet.ext.jaxrs.jar
  • org.restlet.jar

As you can see in the tutorial, you implement your JAXRS resource and application. There are then two different ways to launch the whole web application:

  • Using the built-in Restlet server
  • Using a JavaEE Web container

In order to integrate JAXB support, you first need to understand how representation support of Restlet works. Content of REST requests / responses are contained in representation. Different formats are supported and this is open and extensible. This representation support can be used with converter entities and the converter service.

The converter entity is responsible to convert an element to another one. For example, if you pass a Java instance and you want to transform it as XML to send back in the REST response. The converter service is responsible to handle this conversion in a smart way basing on media type and supporting content negociation (content type defined in Accept and Content-Type headers). Converters are automatically registered when present in classpath.

For JAXB, let's put the following jar in your classpath:

  • activation.jar
  • jaxb-api.jar
  • jsr173_1.0_api.jar

So let's take an example:

  • You send a REST request on your Restlet JAXRS application. You specify the Accept header with value application/xml because you expect an XML content for the response content.

  • In your resource, the corresponding JAXRS method for the request and the required content type returns an object. Restlet will automatically check if there is a registered converter to handle conversion between Java object to XML. If you added the Restlet JAXB extension, it will use it if the Java object is annotated with JAXB annotations.

  • The JAXB converter will use the converter to generate the XML output using JAXB.

  • The response is sent back to the client.

Otherwise, what kind of security do you want to implement? User / password based?

Hope it's clear and it will be helpful!
Thierry

摇划花蜜的午后 2024-11-08 06:41:11

您可能会发现以下示例(来自我的博客)很有用。该示例使用 Jersey 而不是 Restlet,但由于它们都是 JAX-RS 实现,因此应该只有很小的差异。

You may find the following example (from my blog) useful. The example uses Jersey not Restlet, but since they're both JAX-RS implementations there should be only little differences.

旧城烟雨 2024-11-08 06:41:11

我不能和雷斯特莱特说话。

Java 6 附带 JAXB,因此无需安装任何内容。

Glassfish v3.1 Web Profile 预装了 Jersey。因此,您只需下载并启动并运行即可。

I can't speak to Restlet.

Java 6 comes with JAXB, so there's nothing to install for that.

Glassfish v3.1 Web Profile comes with Jersey pre-installed. So you could simply download that and get up and running.

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