You 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.
About this task
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.
You can use the following classes to represent
request entities or response entities:
- org.apache.wink.common.model.atom.AtomEntry
- org.apache.wink.common.model.atom.AtomFeed
- org.apache.wink.common.model.synd.SyndEntry
- org.apache.wink.common.model.synd.SyndFeed
You can also
use these classes as a resource method parameter
to represent an incoming Atom request. Similarly, you can return any
of these classes as a resource response.
Supported configurations: 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.
sptcfg
Procedure
- 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();
}
- 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);
}
- 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;
}
- (optional) If an Atom feed or
entry is sent in a request,
you 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.