Kyle Banks

Creating a Simple RSS Feed for Your Grails Site

Written by @kylewbanks on Mar 16, 2013.

With all of the news about Google shutting down Reader on July 1st, there seems to be an uprising of new RSS feed readers cropping up to try and fill the void.

All of this got me thinking that I should make a quick RSS feed for my site, and came up with the following solution. It's a very simple solution, and should take no longer than a few minutes to set up. The most important parts are to ensure the pubDate format is correct, and to set the response content-type to application/xml.

In your Controller:

def rss = {
    DateFormat pubDateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
    def postList = Post.list(max: 15)

    response.setContentType("application/xml")
    [posts: postList, formatter: pubDateFormatter]
}

And the corresponding view (rss.gsp):


<rss version="2.0">
    <channel>
        <title>Kyle W. Banks</title>
        <link>http://kylewbanks.com/</link>
        <description>A collection of tips, tricks, and tutorials covering a wide range of programming topics.</description>
        <image>
            <url>http://kylewbanks.com/static/images/favicon.ico</url>
            <title>Kyle W. Banks</title>
            <link>http://kylewbanks.com</link>
       </image>
        <g:each in="${posts}" var="post">
            <item>
              <title>${post.title}</title>
              <link>http://kylewbanks.com/blog/${post.url}</link>
              <pubDate>${formatter.format(post.dateCreated)}</pubDate>
              <description><![CDATA[ ${post.body} ]]></description>
            </item>
        </g:each>
    </channel>
</rss>

Finally, add the following link to your site's head to help your RSS feed be discovered by readers, and you're set:

<link rel="alternate" type="application/rss+xml" title="Kyle W. Banks RSS Feed" href="http://kylewbanks.com/rest/rss" />

For an example on how this should look, click here.

I plan to add some tracking to this, and perhaps see if I can somehow get the events into Google Analytics. If I can get Google Analytics to track the server side events, I'll be sure to post back with an update on how I did it.

Let me know if this post was helpful on Twitter @kylewbanks or down below!