With Jersey it's very easy to create a RESTfull resource which returns an XML version of your objects:
@GET
@Path("{id}")
@ProduceMime("application/xml")
public Product getProduct(@PathParam("id") Integer id) {
//...
return product;
}
Jersey uses JAXB to marshall the product object to XML. You can also customize the XML output with jaxb annotations.
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Product implements Serializable {
@XmlAttribute
private Integer Id;
//...
}
This customization will produce an XML output that contains a Product element with an id attribute. Ok now, let's try the service :
curl -v -H "Accept: application/xml" http://localhost:8080/rest-json/resources/product/1
> GET /rest-json/resources/product HTTP/1.1
> User-Agent: curl/7.16.3 (powerpc-apple-darwin8.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
> Host: localhost:8080
> Accept: application/xml
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Content-Type: application/xml
< Content-Length: 200
< Date: Wed, 09 Jul 2008 20:32:10 GMT
<
* Connection #0 to host localhost left intact
* Closing connection #0
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<product id="1">
<name>myphone</name>
<description>A great mobile device !</description>
<category>
<id>1</id>
<name>mobile</name>
</category>
</product>
To produce a JSON output just add the jettison-1.0-RC1.jar file from the jersey distribution to your webapp and change the mime type parameter of the @ProduceMime annotation like this :
@GET
@Path("{id}")
@ProduceMime({"application/json","application/xml"})
public Product getProduct(@PathParam("id") Integer id) {
//...
return product;
}
You have now a REST resource which returns XML or JSON, the response format depends of the Accept header of the HTTP request :
curl -v -H "Accept: application/json" http://localhost:8080/rest-json/resources/product/1
> GET /rest-json/resources/product/1 HTTP/1.1
> User-Agent: curl/7.16.3 (powerpc-apple-darwin8.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
> Host: localhost:8080
> Accept: application/json
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Wed, 09 Jul 2008 20:47:26 GMT
<
* Connection #0 to host localhost left intact
* Closing connection #0
{"product":{"@id":"1","name":"myphone",
"description":"A great mobile device !",
"category":{"id":"1","name":"mobile"}}}
It's very easy and powerful ! But if you look in details the JSON output you'll notice that the Product's property
id is ""@id":"1"" and not ""id":1" ?!
In this case the Product object is marshalling by JAXB and converted to JSON by JETTISON. The returned JSON is mapped on the XML object representation.
If you want to produce a more simpler JSON like that :
{"product":{"id":1,"name":"myphone"}
You have to remove all JAXB annotations from the Product classe. You'll lose the XML customization and in some cases the XML output could be too verbose or not readable (for human ).
You can also use a JSON framework to manage the marshalling process ...
see the part II :-)