Beyond the @Produces annotation, how does Jersey (JAX-RS) know to treat a POJO as a specific mime ty

Posted by hal10001 on Stack Overflow See other posts from Stack Overflow or by hal10001
Published on 2010-04-30T13:53:24Z Indexed on 2010/04/30 13:57 UTC
Read the original article Hit count: 357

Filed under:
|
|
|

I see a lot of examples for Jersey that look something like this:

public class ItemResource {

    @GET
    @Path("/items")
    @Produces({"text/xml", "application/json"})
    public List<Item> getItems() {
        List<Item> items = new ArrayList<Item>();

        Item item = new Item();
        item.setItemName("My Item Name!");
        items.add(item);

        return items;
    }
}

But then I have trouble dissecting Item, and how Jersey knows how to translate an Item to either XML or JSON. I've seen very basic examples that just return a String of constructed HTML or XML, which makes more sense to me, but I'm missing the next step. I looked at the samples, and one of them stood out (the json-from-jaxb sample), since the object was marked with these types of annotations:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "flight"
})
@XmlRootElement(name = "flights")

I'm looking for tutorials that cover this "translation" step-by-step, or an explanation here of how to translate a POJO to output as a specific mime type. Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about jersey