RSS 和 Atom

AbstractAtomFeedViewAbstractRssFeedView 都继承自 AbstractFeedView 基类,分别用于提供 Atom 和 RSS Feed 视图。它们基于 ROME 项目,位于 org.springframework.web.servlet.view.feed 包中。

AbstractAtomFeedView 要求你实现 buildFeedEntries() 方法,并可以选择覆盖 buildFeedMetadata() 方法(默认实现为空)。以下示例展示了如何实现

  • Java

  • Kotlin

public class SampleContentAtomView extends AbstractAtomFeedView {

	@Override
	protected void buildFeedMetadata(Map<String, Object> model,
			Feed feed, HttpServletRequest request) {
		// implementation omitted
	}

	@Override
	protected List<Entry> buildFeedEntries(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// implementation omitted
	}
}
class SampleContentAtomView : AbstractAtomFeedView() {

	override fun buildFeedMetadata(model: Map<String, Any>,
			feed: Feed, request: HttpServletRequest) {
		// implementation omitted
	}

	override fun buildFeedEntries(model: Map<String, Any>,
			request: HttpServletRequest, response: HttpServletResponse): List<Entry> {
		// implementation omitted
	}
}

实现 AbstractRssFeedView 也有类似的要求,如下例所示

  • Java

  • Kotlin

public class SampleContentRssView extends AbstractRssFeedView {

	@Override
	protected void buildFeedMetadata(Map<String, Object> model,
			Channel feed, HttpServletRequest request) {
		// implementation omitted
	}

	@Override
	protected List<Item> buildFeedItems(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// implementation omitted
	}
}
class SampleContentRssView : AbstractRssFeedView() {

	override fun buildFeedMetadata(model: Map<String, Any>,
								feed: Channel, request: HttpServletRequest) {
		// implementation omitted
	}

	override fun buildFeedItems(model: Map<String, Any>,
			request: HttpServletRequest, response: HttpServletResponse): List<Item> {
		// implementation omitted
	}
}

buildFeedItems()buildFeedEntries() 方法会传入 HTTP 请求,以防你需要访问区域设置。HTTP 响应仅用于设置 cookie 或其他 HTTP 标头。方法返回后,Feed 会自动写入响应对象。

有关创建 Atom 视图的示例,请参阅 Alef Arendsen 的 Spring Team 博客 条目