How to set up a Restlet server with JAXRS and JAXB?
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.
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
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.
- Part 1 - The Database
- Part 2 - JPA Entities
- Part 3 - JAXB Bindings
- Part 4 - The RESTFul Service
- Part 5 - The Client
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.
精彩评论