WAS v8.5 > End-to-end paths > Web services - RESTful services > Use Atom content in JAX-RS application requests and responses > Step 4. Implement JAXB-based ATOM content for requests and responses

Use the JAXB-based Atom model for requests and responses

We can represent Atom documents using JAXB annotated objects because the Atom format is based on XML. Therefore, one way to implement a resource method that consumes and produces Atom feeds and Atom entries is to return a JAXB annotated object that uses the Atom model. The JAX-RS library provides an Atom model based on JAXB. A JAXB Atom model is included in the Apache Wink-based IBM JAX-RS library JAR file. Since the model is based on JAXB, the objects automatically use the built-in JAXB entity providers for reading and writing Atom feeds and Atom entries. See the API information for details on the methods in the Atom model.

We can use the following classes to represent request entities or response entities:

We can also use these classes as a resource method parameter to represent an incoming Atom request. Similarly, we can return any of these classes as a resource response.

There are some third-party libraries that support JAXB to JSON functionality.

The JAXB Atom model uses features of JAXB that might not be supported by the third-party JAXB to JSON libraries.

  1. Create a new AtomFeed object that returns an Atom feed as the response message body in your resource method.
    @javax.ws.rs.GET
    public org.apache.wink.common.model.atom.AtomFeed getFeed() {
        org.apache.wink.common.model.atom.AtomFeed feed = new org.apache.wink.common.model.atom.AtomFeed();}
  2. To add information to the feed, call methods on the AtomFeed Java object, and add AtomEntry objects to the list of feeds.
    @javax.ws.rs.GET
    public org.apache.wink.common.model.atom.AtomFeed getFeed() {
            org.apache.wink.common.model.atom.AtomFeed feed = new org.apache.wink.common.model.atom.AtomFeed();
            feed.setTitle(new org.apache.wink.common.model.atom.AtomText("Feed Title"));
            
            org.apache.wink.common.model.atom.AtomEntry entry = new org.apache.wink.common.model.atom.AtomEntry();
            entry.setTitle(new org.apache.wink.common.model.atom.AtomText("Entry Title"));
            entry.setId("1");
            
            org.apache.wink.common.model.atom.AtomLink link = new org.apache.wink.common.model.atom.AtomLink();
            link.setHref("http://www.example.com/");
            List< org.apache.wink.common.model.atom.AtomLink> entryLinks = entry.getLinks();
            entryLinks.add(link);
            
            List< org.apache.wink.common.model.atom.AtomEntry> entries = af.getEntries();
            entries.add(entry);}

  3. Return the feed in the Java method.
    @javax.ws.rs.GET
    public org.apache.wink.common.model.atom.AtomFeed getFeed() {
            org.apache.wink.common.model.atom.AtomFeed feed = new org.apache.wink.common.model.atom.AtomFeed();
            feed.setTitle(new org.apache.wink.common.model.atom.AtomText("Feed Title"));
            
            org.apache.wink.common.model.atom.AtomEntry entry = new org.apache.wink.common.model.atom.AtomEntry();
            entry.setTitle(new org.apache.wink.common.model.atom.AtomText("Entry Title"));
            entry.setId("1");
            
            org.apache.wink.common.model.atom.AtomLink link = new org.apache.wink.common.model.atom.AtomLink();
            link.setHref("http://www.example.com/");
            List< org.apache.wink.common.model.atom.AtomLink> entryLinks = entry.getLinks();
            entryLinks.add(link);
            
            List<org.apache.wink.common.model.atom.AtomEntry> entries = af.getEntries();
            entries.add(entry);
            return entries;}
  4. (optional) If an Atom feed or entry is sent in a request, we can add one of the Atom types as a parameter to the resource method. By adding one of the Atom types as a parameter to the resource method, any incoming Atom feed data is passed as the parameter.
    @javax.ws.rs.POST
    public void postFeed(org.apache.wink.common.model.atom.AtomFeed incomingFeed) {
        // use the incomingFeed object}


Results

You have used the Atom JAXB model to represent request and response entities.


Subtopics


Related


Use Atom content in JAX-RS application requests and responses


+

Search Tips   |   Advanced Search