如何使用JAXRS和JAXB设置Restlet服务器? [英] How to set up a Restlet server with JAXRS and JAXB?

查看:107
本文介绍了如何使用JAXRS和JAXB设置Restlet服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找互联网,试图找到一个如何做到这一点的例子。我只想设置一个REST服务器,它自动将对象与XML进行序列化。我只是尝试提供一个服务器,只有在用户登录后才能使用户登录,注销和访问XML对象列表。获得一个简单的示例应用程序需要什么?

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?

我没有理解Restlet库的工作方式,而且我完全不习惯使用JAXB和JAXRS。我参与了一个使用这些库的项目,但只是从客户的角度来看。

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.

推荐答案

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

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

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

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

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

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:


  • 使用内置的Restlet服务器

  • 使用JavaEE Web容器

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

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.

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

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.

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

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


  • activation.jar

  • jaxb-api.jar

  • jsr173_1.0_api.jar

让我们举一个例子:


  • 你发送一个您的Restlet JAXRS应用程序上的REST请求。您可以使用值application / xml指定Accept标头,因为您需要响应内容的XML内容。

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

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

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.

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

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

将响应发送回客户端。

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

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

希望它清楚,它会有所帮助!
Thierry

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

这篇关于如何使用JAXRS和JAXB设置Restlet服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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