<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring Boot RSS feed 和 ROAM > [https://howtodoinjava.com/spring-boot/spring-boot-rome-rss-and-atom-feed/](https://howtodoinjava.com/spring-boot/spring-boot-rome-rss-and-atom-feed/) 在本教程中,學習從 SpringBoot 應用程序**創建和使用 RSS 和 Atom 提要**。 您一定已經在[各種網站](https://howtodoinjava.com/feed/)(如 RSS 提要之類的)上以文本或圖像按鈕的形式看到了這一點,并邀請您“通過 RSS 訂閱”。RSS 是簡單的聯合 API,通常稱為富站點摘要。 RSS 徹底改變了用戶與在線內容進行交互的方式。 與 RSS 相似,Atom 也是基于 XML 的 Web 內容和元數據聯合格式,并且是用于發布和編輯屬于定期更新的網站的 Web 資源的應用程序級協議。 所有 Atom 提要必須是格式為`application/atom+xml`的 XML 文檔。 ## 總覽 在這個運行示例中,我們將使用 Spring Boot API 公開 RSS 和 ATOM 的兩個簡單端點,并且我們將了解如何使用 java 客戶端使用這些提要。 #### 技術棧 * JDK 1.8,Eclipse,Maven – 開發環境 * SpringBoot – 基礎應用程序框架 * [ROME 庫](https://github.com/rometools/rome) – 用于發布提要 ## 怎么運行的? 基本上,使用 Spring 框架發布 RSS 或 Atom 提要非常容易。 在 spring 框架中,有兩個 http 消息轉換器([`RssChannelHttpMessageConverter`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/feed/RssChannelHttpMessageConverter.html)和[`AtomFeedHttpMessageConverter`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/feed/AtomFeedHttpMessageConverter.html))可以將 spring 控制器方法的響應轉換為 XML feed 格式(如果返回類型與以下任何一種相關) 飼料。 這兩個轉換器都依賴于 ROME 庫,當 Spring 框架在類路徑上發現庫時,Spring 框架會自動注冊這兩個轉換器。 我們要做的就是將 ROME 庫添加為`pom.xml`的依賴。 ## 項目結構 下面給出了為此演示創建的類和文件。 ![](https://img.kancloud.cn/09/4e/094e063401e38381e487b9d386701355_434x417.jpg) 項目 ## 創建 RSS / ATOM 提要生成器 #### 創建 Spring Boot 項目 首先從[ Spring 初始化器](https://start.spring.io/)站點創建一個僅具有`Web`依賴項的 spring boot 項目。 選擇依賴項并提供適當的 Maven GAV 坐標后,以壓縮格式下載項目。 解壓縮,然后將 eclipse 中的項目導入為 maven 項目。 ![](https://img.kancloud.cn/14/20/1420fb8099309b2e55d8cfc7ac31ea10_1365x702.jpg) Spring boot 項目生成 #### 添加 ROAM 依賴關系 現在,我們需要在新創建的項目的`pom.xml`中添加 ROAM 依賴項。 ```java <dependency> <groupId>com.rometools</groupId> <artifactId>rome</artifactId> <version>1.8.0</version> </dependency> ``` ## 創建控制器 現在添加一個 Spring 控制器并添加兩個端點`/rss`和`/atom`分別公開 RSS 和 Atom 提要。 正如我們已經提到的,僅添加此控制器將自動適用于我們的情況,因為內部 spring 框架將注冊兩個 http 消息轉換器(`RssChannelHttpMessageConverter`和`AtomFeedHttpMessageConverter`),一旦我們在類路徑中具有 ROAM 依賴項,它們便將被注冊。 我們唯一需要做的就是從控制器方法中返回正確的`Feed`類型的對象。 在我們的情況下,提要對象的 RSS 類型為`com.rometools.rome.feed.rss.Channel`,Atom 類型為`com.rometools.rome.feed.atom.Feed`。 因此,在添加提要的內容以及有關通道的其他詳細信息之后,我們的控制器將如下所示。 ```java package com.example.howtodoinjava.rss; import java.util.Collections; import java.util.Date; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.rometools.rome.feed.atom.Category; import com.rometools.rome.feed.atom.Content; import com.rometools.rome.feed.atom.Entry; import com.rometools.rome.feed.atom.Feed; import com.rometools.rome.feed.atom.Link; import com.rometools.rome.feed.atom.Person; 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; @RestController public class FeedController { @GetMapping(path = "/rss") public Channel rss() { Channel channel = new Channel(); channel.setFeedType("rss_2.0"); channel.setTitle("HowToDoInJava Feed"); channel.setDescription("Different Articles on latest technology"); channel.setLink("https://howtodoinjava.com"); channel.setUri("https://howtodoinjava.com"); channel.setGenerator("In House Programming"); Image image = new Image(); image.setUrl("https://howtodoinjava.com/wp-content/uploads/2015/05/howtodoinjava_logo-55696c1cv1_site_icon-32x32.png"); image.setTitle("HowToDoInJava Feed"); image.setHeight(32); image.setWidth(32); channel.setImage(image); Date postDate = new Date(); channel.setPubDate(postDate); Item item = new Item(); item.setAuthor("Lokesh Gupta"); item.setLink("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/"); item.setTitle("Spring CORS Configuration Examples"); item.setUri("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/"); item.setComments("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/#respond"); 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( "CORS helps in serving web content from multiple domains into browsers who usually have the same-origin security policy. In this example, we will learn to enable CORS support in Spring MVC application at method and global level." + "The post <a rel=\"nofollow\" href=\"https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/\">Spring CORS Configuration Examples</a> appeared first on <a rel=\"nofollow\" href=\"https://howtodoinjava.com\">HowToDoInJava</a>."); item.setDescription(descr); item.setPubDate(postDate); channel.setItems(Collections.singletonList(item)); //Like more Entries here about different new topics return channel; } @GetMapping(path = "/atom") public Feed atom() { Feed feed = new Feed(); feed.setFeedType("atom_1.0"); feed.setTitle("HowToDoInJava"); feed.setId("https://howtodoinjava.com"); Content subtitle = new Content(); subtitle.setType("text/plain"); subtitle.setValue("Different Articles on latest technology"); feed.setSubtitle(subtitle); Date postDate = new Date(); feed.setUpdated(postDate); Entry entry = new Entry(); Link link = new Link(); link.setHref("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/"); entry.setAlternateLinks(Collections.singletonList(link)); SyndPerson author = new Person(); author.setName("Lokesh Gupta"); entry.setAuthors(Collections.singletonList(author)); entry.setCreated(postDate); entry.setPublished(postDate); entry.setUpdated(postDate); entry.setId("https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/"); entry.setTitle("spring-mvc-cors-configuration"); Category category = new Category(); category.setTerm("CORS"); entry.setCategories(Collections.singletonList(category)); Content summary = new Content(); summary.setType("text/plain"); summary.setValue( "CORS helps in serving web content from multiple domains into browsers who usually have the same-origin security policy. In this example, we will learn to enable CORS support in Spring MVC application at method and global level." + "The post <a rel=\"nofollow\" href=\"https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/\">Spring CORS Configuration Examples</a> appeared first on <a rel=\"nofollow\" href=\"https://howtodoinjava.com\">HowToDoInJava</a>."); entry.setSummary(summary); feed.setEntries(Collections.singletonList(entry)); //Like more Entries here about different new topics return feed; } } ``` #### 示例 啟動 spring boot 應用程序,使用`mvn clean install`進行 maven 構建,然后使用`java -jar target\spring-boot-rss-feed-example-0.0.1-SNAPSHOT.jar`命令啟動應用程序。 這將在默認端口`8080`中啟動一臺 tomcat 服務器,并將在其中部署應用程序。 現在,從瀏覽器轉到 [http://localhost:8080/rss](http://localhost:8080/rss) 和 [http://localhost:8080/atom](http://localhost:8080/atom),您應該會看到 RSS 和 Atom 提要中的主題已添加到控制器中。 ![RSS Feed](https://img.kancloud.cn/16/3a/163a455ab70d31db41a715ce18b088e8_1327x512.jpg) RSS feed ![Atom Feed](https://img.kancloud.cn/de/aa/deaa8436eb3e1ebd6181ed2287ff875c_1360x423.jpg) Atom Feed ## 創建 RSS Feed 閱讀器 我們已經有很多 Feed 閱讀器可用,但是如果您需要以編程方式使用此 Feed,也可以使用 ROAM 庫通過以下幾行代碼來完成此操作。 ```java package com.example.howtodoinjava.rss; import java.net.URL; import com.rometools.rome.feed.synd.SyndEntry; import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.XmlReader; public class FeedConsumer { public static void main(String[] args) { try { String url = "http://localhost:8080/rss"; try (XmlReader reader = new XmlReader(new URL(url))) { SyndFeed feed = new SyndFeedInput().build(reader); System.out.println(feed.getTitle()); System.out.println("***********************************"); for (SyndEntry entry : feed.getEntries()) { System.out.println(entry); System.out.println("***********************************"); } System.out.println("Done"); } } catch (Exception e) { e.printStackTrace(); } } } ``` 這里的 URL 用于 RSS feed,如果將 URL 更改為 Atom feed,同樣的代碼也可以工作。 **輸出** 這是 feed 客戶端的控制臺輸出。 ```java HowToDoInJava Feed *********************************** SyndEntryImpl.comments=https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/#respond SyndEntryImpl.author=Lokesh Gupta SyndEntryImpl.wireEntry=null SyndEntryImpl.link=https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/ SyndEntryImpl.description.mode=null SyndEntryImpl.description.type=text/html SyndEntryImpl.description.interface=interface com.rometools.rome.feed.synd.SyndContent SyndEntryImpl.description.value=CORS helps in serving web content from multiple domains into browsers who usually have the same-origin security policy. In this example, we will learn to enable CORS support in Spring MVC application at method and global level.The post <a rel="nofollow" href="https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/">Spring CORS Configuration Examples</a> appeared first on <a rel="nofollow" href="https://howtodoinjava.com">HowToDoInJava</a>. SyndEntryImpl.foreignMarkup=[] SyndEntryImpl.source=null SyndEntryImpl.updatedDate=null SyndEntryImpl.title=Spring CORS Configuration Examples SyndEntryImpl.interface=interface com.rometools.rome.feed.synd.SyndEntry SyndEntryImpl.uri=https://howtodoinjava.com/spring5/webmvc/spring-mvc-cors-configuration/ SyndEntryImpl.enclosures=[] SyndEntryImpl.modules[0].date=Sat Oct 14 10:57:12 IST 2017 SyndEntryImpl.modules[0].formats=[] SyndEntryImpl.modules[0].sources=[] SyndEntryImpl.modules[0].rightsList=[] SyndEntryImpl.modules[0].subject=null SyndEntryImpl.modules[0].creators[0]=Lokesh Gupta SyndEntryImpl.modules[0].description=null SyndEntryImpl.modules[0].language=null SyndEntryImpl.modules[0].source=null SyndEntryImpl.modules[0].type=null SyndEntryImpl.modules[0].title=null SyndEntryImpl.modules[0].interface=interface com.rometools.rome.feed.module.DCModule SyndEntryImpl.modules[0].descriptions=[] SyndEntryImpl.modules[0].coverages=[] SyndEntryImpl.modules[0].relation=null SyndEntryImpl.modules[0].contributor=null SyndEntryImpl.modules[0].rights=null SyndEntryImpl.modules[0].publishers=[] SyndEntryImpl.modules[0].coverage=null SyndEntryImpl.modules[0].identifier=null SyndEntryImpl.modules[0].creator=Lokesh Gupta SyndEntryImpl.modules[0].types=[] SyndEntryImpl.modules[0].languages=[] SyndEntryImpl.modules[0].identifiers=[] SyndEntryImpl.modules[0].subjects=[] SyndEntryImpl.modules[0].format=null SyndEntryImpl.modules[0].dates[0]=Sat Oct 14 10:57:12 IST 2017 SyndEntryImpl.modules[0].titles=[] SyndEntryImpl.modules[0].uri=http://purl.org/dc/elements/1.1/ SyndEntryImpl.modules[0].publisher=null SyndEntryImpl.modules[0].contributors=[] SyndEntryImpl.modules[0].relations=[] SyndEntryImpl.contents=[] SyndEntryImpl.links=[] SyndEntryImpl.publishedDate=Sat Oct 14 10:57:12 IST 2017 SyndEntryImpl.contributors=[] SyndEntryImpl.categories[0].taxonomyUri=null SyndEntryImpl.categories[0].name=CORS SyndEntryImpl.categories[0].interface=interface com.rometools.rome.feed.synd.SyndCategory SyndEntryImpl.titleEx.mode=null SyndEntryImpl.titleEx.type=null SyndEntryImpl.titleEx.interface=interface com.rometools.rome.feed.synd.SyndContent SyndEntryImpl.titleEx.value=Spring CORS Configuration Examples SyndEntryImpl.authors=[] *********************************** Done ``` ## 總結 在此示例中,我們了解到如何輕松地將 RSS 和 Atom feed 配置到我們的 spring boot 項目中。 我們也看到了如何從 Java 代碼中使用它們。 就是今天的話題。 我在這里附上完整的 Eclipse 項目,以供您參考。 請在評論部分添加您的反饋。 [下載源碼](https://howtodoinjava.com/wp-content/uploads/2017/10/spring-boot-rss-feed-example.zip) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看