3.12 Create an RSS feed
Add wanted path to router.groovy
is this needed if the path is also in the resource?
resource(uri: "/feed",
ofClass: "org.kauriproject.myblog.feed.BlogItemsResource");
Create a BlogItemResource
We'll create a resource class that can get a list of all blog items, this method puts this list into a hashmap. Such a method returns a KauriRepresentation, which on his turn will be mapped to a template in representations.groovy. We create a "listBlogItems" method in this resource class, annotated with a jaxrs path. This method uses JPA to create a list of blogitems.
package org.kauriproject.myblog.feed;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.kauriproject.myblog.entities.BlogItem;
import org.kauriproject.representation.build.KauriRepresentation;
import org.restlet.resource.Resource;
import org.springframework.orm.jpa.support.JpaDaoSupport;
@Path("/feed")
public class BlogItemsResource extends Resource{
private JpaDaoSupport jpaDaoSupport;
public void setJpaDaoSupport(JpaDaoSupport jpaDaoSupport) {
this.jpaDaoSupport = jpaDaoSupport;
}
@Path("list")
@GET
@Produces("application/xml")
public KauriRepresentation listBlogItems() {
Map<String, Object> data = new HashMap<String, Object>();
// load all blogitems into data
List<BlogItem> items = jpaDaoSupport.getJpaTemplate().find("select b from items b");
data.put("items", items);
return new KauriRepresentation("atom", data);
}
}
Add template to representations.groovy
The resource class creates a Kauri representation which in mapped in representations.groovy:
when(name: "atom") {
template(src: "module:/pages/atom.xml")
}
Create atom template
pages/atom.xml explain
Previous