Spring Boot集成rss快速入门demo

2024-05-29 09:44

本文主要是介绍Spring Boot集成rss快速入门demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.什么是rss?

RSS 的全称是「简易内容聚合」(Really Simple Syndication),是一个能让你在一个地方订阅各种感兴趣网站的工具。

一个网站支持 RSS,就意味着每当它新发布一篇新文章,就会往一个位于特定网址的文件中,以特定的语法(具体而言是 XML 标记语言或 JSON)增加一条记录,列明这篇文章的标题、作者、发表时间和内容(可以是全文,也可以是摘要)等信息。这样,用户只要搜集所有他感兴趣的网站提供的这种文件的网址,并不时检查这些文件内容的更新,就能知道这些网站是否、何时发布了什么内容。RSS 阅读器的核心功能,就是存储用户订阅的 RSS 地址,以固定的频率自动检查更新,并将其内容转换为易读的格式呈现给用户。

为什么用 RSS

RSS 的对立面是算法推荐,像微信公众号、知乎、微博、今日头条等平台。 且不说算法推送平台广告多,迁移麻烦的问题。算法推荐的特点是,你不需要刻意选择,算法会根据你的喜好,给你推送内容。这样一来,你几乎没有选择的余地,在不断被「喂饱」中逐渐失去判断的能力。更可怕的地方在于,它替你定义了你的画像,然后把你潜移默化中变成了它所认为的你。「大数据杀熟」的东窗事发绝非偶然,用算法窥视用户隐私是当今互联网公司的通配。

做信息的主人,而不是奴隶。RSS 是一种公开的协议,可自由更换平台与客户端。重要的一点是,获取信息的权力完全自治。RSS 相比算法推荐,拥有了可控性和安全感,隐私完全掌握在自己手里。

什么是atom?

Atom是一对彼此相关的标准。Atom供稿格式(Atom Syndication Format)是用于网站消息来源,基于XML的文档格式;而Atom出版协定(Atom Publishing Protocol,简称AtomPub或APP)是用于新增及修改网络资源,基于HTTP的协议。

它借鉴了各种版本RSS的使用经验,被许多的聚合工具广泛使用在发布和使用上。Atom供稿格式设计作为RSS的替代品;而Atom出版协定用来取 代现有的多种发布方式(如Blogger API和LiveJournal XML-RPC Client/Server Protocol)。而值得一提的是Google提供的多种服务正在使用Atom。Google Data API(GData)亦基于Atom。

Atom是IETF的"建议标准",Atom供稿格式列为RFC 4287,而Atom出版协定列为RFC 5023。

2.代码工程

实验目标:实现rss和atom订阅源

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>rss</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--ROAM依赖  RSS 订阅--><dependency><groupId>com.rometools</groupId><artifactId>rome</artifactId><version>1.15.0</version></dependency><!-- https://mvnrepository.com/artifact/com.rometools/rome-utils --><dependency><groupId>com.rometools</groupId><artifactId>rome-utils</artifactId><version>1.15.0</version></dependency><dependency><groupId>org.jdom</groupId><artifactId>jdom2</artifactId><version>2.0.6</version></dependency></dependencies>
</project>

controller

采用rome库实现rss和atom订阅

package com.et.rss.controller;import com.rometools.rome.feed.atom.*;
import com.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Description;
import com.rometools.rome.feed.rss.Image;
import com.rometools.rome.feed.rss.Item;
import com.rometools.rome.feed.synd.SyndPerson;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.Date;@RestController
@RequestMapping("/feed")
public class FeedController {@GetMapping(path = "/rss")public Channel rss() {Channel channel = new Channel();channel.setFeedType("rss_2.0");channel.setTitle("HBLOG Feed");channel.setDescription("Recent posts");channel.setLink("http://www.liuhaihua.cn");channel.setUri("http://www.liuhaihua.cn");channel.setGenerator("Harries");Image image = new Image();image.setUrl("http://www.liuhaihua.cn/img/hblog.png");image.setTitle("HBLOG Feed");image.setHeight(32);image.setWidth(32);channel.setImage(image);Date postDate = new Date();channel.setPubDate(postDate);Item item = new Item();item.setAuthor("Harries");item.setLink("http://www.liuhaihua.cn/archives/710608.html");item.setTitle("Spring Boot integrated banner   quick start demo");item.setUri("http://www.liuhaihua.cn/archives/710608.html");item.setComments("http://www.liuhaihua.cn/archives/710608.html#commnet");com.rometools.rome.feed.rss.Category category = new com.rometools.rome.feed.rss.Category();category.setValue("CORS");item.setCategories(Collections.singletonList(category));Description descr = new Description();descr.setValue("pring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information");item.setDescription(descr);item.setPubDate(postDate);channel.setItems(Collections.singletonList(item));//Like more Entries here about different new topicsreturn channel;}@GetMapping(path = "/atom")public Feed atom() {Feed feed = new Feed();feed.setFeedType("atom_1.0");feed.setTitle("HBLOG");feed.setId("http://www.liuhaihua.cn");Content subtitle = new Content();subtitle.setType("text/plain");subtitle.setValue("recents post");feed.setSubtitle(subtitle);Date postDate = new Date();feed.setUpdated(postDate);Entry entry = new Entry();Link link = new Link();link.setHref("http://www.liuhaihua.cn/archives/710608.html");entry.setAlternateLinks(Collections.singletonList(link));SyndPerson author = new Person();author.setName("HBLOG");entry.setAuthors(Collections.singletonList(author));entry.setCreated(postDate);entry.setPublished(postDate);entry.setUpdated(postDate);entry.setId("710608");entry.setTitle("Spring Boot integrated banner   quick start demo");Category category = new Category();category.setTerm("CORS");entry.setCategories(Collections.singletonList(category));Content summary = new Content();summary.setType("text/plain");summary.setValue("Spring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information");entry.setSummary(summary);feed.setEntries(Collections.singletonList(entry));//add different topicreturn feed;}}

DemoApplication.java

package com.et.rss;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

application.yaml

server:port: 8088

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.

3.测试

启动Spring Boot应用

rss

http://127.0.0.1:8088/feed/rss

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><title>HBLOG Feed</title><link>http://www.liuhaihua.cn</link><description>Recent posts</description><pubDate>Fri, 24 May 2024 14:26:21 GMT</pubDate><generator>Harries</generator><image><title>HBLOG Feed</title><url>http://www.liuhaihua.cn/img/hblog.png</url><width>32</width><height>32</height></image><item><title>Spring Boot integrated banner   quick start demo</title><link>http://www.liuhaihua.cn/archives/710608.html</link><description>pring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information</description><category>CORS</category><pubDate>Fri, 24 May 2024 14:26:21 GMT</pubDate><author>Harries</author><comments>http://www.liuhaihua.cn/archives/710608.html#commnet</comments></item></channel>
</rss>

atom

http://127.0.0.1:8088/feed/atom

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>HBLOG</title><subtitle type="text">recents post</subtitle><id>http://www.liuhaihua.cn</id><updated>2024-05-24T14:25:38Z</updated><entry><title>Spring Boot integrated banner   quick start demo</title><link rel="alternate" href="http://www.liuhaihua.cn/archives/710608.html" /><category term="CORS" /><author><name>HBLOG</name></author><id>710608</id><updated>2024-05-24T14:25:38Z</updated><published>2024-05-24T14:25:38Z</published><summary type="text">Spring Boot Banner is a feature for displaying custom ASCII art and information at application startup. This ASCII art usually includes the project name, version number, author information</summary></entry>
</feed>

4.引用

  • Spring boot RSS feed with rome
  • Spring Boot集成rss快速入门demo | Harries Blog™

 

这篇关于Spring Boot集成rss快速入门demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1013321

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.