Besides the @Produces annotation, how does Jersey (JAX-RS) know how to handle POJOs as a specific MIME type?

I see many 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 problems parsing the Item, and how Jersey knows how to translate the Item into XML or JSON. I saw very simple examples that simply return the String of the constructed HTML or XML, which makes more sense to me, but I skip 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 am looking for tutorials that cover this "translation" step by step, or an explanation here on how to translate a POJO for output as a specific mime type. Thank!

+3
1

. -, @Produces . Accept, , @Produces . , /xml .

Jersey MessageBodyWriter, Item text/xml. "mapper", XML JSON Jersey MessageBodyReaders .

, - .

Jan

+10

Source: https://habr.com/ru/post/1743516/


All Articles