Fetching an RSS Feed with ROME

May 17, 2019 17:49 · 155 words · 1 minute read

A while ago ROME had a Fetcher module which became deprecated. This snippet is a template for doing a conditional GET with Spring’s Rest Template against an RSS feed’s URL.

SyndFeed feed = restTemplate.execute(/* URL */, HttpMethod.GET,
request -> {
    request.getHeaders().add("If-Modified-Since", /* If modified since header value */);
    request.getHeaders().add("If-None-Match", /* etag header value */);
}, response -> {
    List<String> eTagValues = response.getHeaders().get("etag");

    if (eTagValues != null) {
        eTagValues.stream().findFirst().ifPresent(value -> {
            /* This is the etag value, update your records with this */
        });
    }

    SyndFeedInput input = new SyndFeedInput();
    try {
        return input.build(new XmlReader(response.getBody()));
    } catch (FeedException e) {
        // Log exception or re-throw
    }
    return null;
});

Note also that you’ll need to send the If-Modified-Since header in the required format which you can do with SimpleDateFormatter

SimpleDateFormat modifiedSinceDateFormat = new SimpleDateFormat("E, dd MMM YYYY HH:mm:ss")
modifiedSinceDateFormat.format(new Date()) + " GMT";

The original Github issue for deprecation has more examples to replace the fetcher.