From 1b0e859d3a85970462c7daefc646e37155b87c0b Mon Sep 17 00:00:00 2001 From: Dassi orleando Date: Sun, 18 Feb 2018 00:08:19 +0100 Subject: [PATCH] BAEL-1273: RSS improvements with a custom model (#3665) * BAEL-1216: improve tests * BAEL-1448: Update Spring 5 articles to use the release version * Setting up the Maven Wrapper on a maven project * Add Maven Wrapper on spring-boot module * simple add * BAEL-976: Update spring version * BAEL-1273: Display RSS feed with spring mvc (AbstractRssFeedView) * Move RSS feed with Spring MVC from spring-boot to spring-mvc-simple * BAEL-1285: Update Jackson articles * BAEL-1273: implement both MVC and Rest approach to serve RSS content * RSS(XML & Json) with a custom model * BAEL-1273: remove a resource --- spring-mvc-simple/pom.xml | 24 ++++++++ .../ApplicationConfiguration.java | 30 ++++++++-- .../spring/controller/rss/ArticleFeed.java | 27 +++++++++ .../spring/controller/rss/ArticleItem.java | 25 +++++++++ .../controller/rss/ArticleRssController.java | 30 ++++------ .../spring/controller/rss/RssData.java | 56 +++++++++++++++++++ 6 files changed, 169 insertions(+), 23 deletions(-) create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java create mode 100644 spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java diff --git a/spring-mvc-simple/pom.xml b/spring-mvc-simple/pom.xml index 595e58f5f3..e722573ab1 100644 --- a/spring-mvc-simple/pom.xml +++ b/spring-mvc-simple/pom.xml @@ -31,9 +31,16 @@ 5.0.2 1.0.2 1.9.0 + 2.9.4 + 1.4.9 + + org.springframework + spring-oxm + 5.0.2.RELEASE + javax.servlet javax.servlet-api @@ -121,6 +128,23 @@ rome ${rome.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + com.thoughtworks.xstream + xstream + ${xstream.version} + diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java index 69c45d90b3..7f1182bb50 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java @@ -1,26 +1,33 @@ package com.baeldung.spring.configuration; import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.feed.RssChannelHttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter; +import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.accept.ContentNegotiationManager; -import java.util.List; import java.util.ArrayList; +import java.util.List; @Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" }) -class ApplicationConfiguration implements WebMvcConfigurer { +class ApplicationConfiguration extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { @@ -49,4 +56,19 @@ class ApplicationConfiguration implements WebMvcConfigurer { multipartResolver.setMaxUploadSize(5242880); return multipartResolver; } + + @Override + public void configureMessageConverters(List> converters) { + Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml(); + builder.indentOutput(true); + + XmlMapper xmlMapper = builder.createXmlMapper(true).build(); + xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); + + converters.add(new RssChannelHttpMessageConverter()); + converters.add(new MappingJackson2HttpMessageConverter()); + converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper)); + + super.configureMessageConverters(converters); + } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java new file mode 100644 index 0000000000..514c9a2353 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleFeed.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.controller.rss; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +@JacksonXmlRootElement(localName="articles") +public class ArticleFeed extends RssData implements Serializable { + + @JacksonXmlElementWrapper(localName = "items", useWrapping = true) + private List items = new ArrayList(); + + public void addItem(ArticleItem articleItem) { + this.items.add(articleItem); + } + + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java new file mode 100644 index 0000000000..01b2cde1ba --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleItem.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.controller.rss; + +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; + +import java.io.Serializable; + +@JacksonXmlRootElement(localName="article") +public class ArticleItem extends RssData implements Serializable { + private String author; + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public String toString() { + return "ArticleItem{" + + "author='" + author + '\'' + + '}'; + } +} diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java index 8f23076e8e..77b8aceb73 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java @@ -14,46 +14,38 @@ import java.util.List; @Controller public class ArticleRssController { - @GetMapping(value = "/rssMvc") + @GetMapping(value = "/rss1") public String articleMvcFeed() { return "articleFeedView"; } - @GetMapping(value = "/rssRest", produces = "application/rss+xml") + @GetMapping(value = "/rss2") @ResponseBody - public String articleRestFeed() throws FeedException { - SyndFeed feed = new SyndFeedImpl(); - feed.setFeedType("rss_2.0"); + public ArticleFeed articleRestFeed2() { + ArticleFeed feed = new ArticleFeed(); feed.setLink("http://localhost:8080/spring-mvc-simple/rss"); feed.setTitle("Article Feed"); feed.setDescription("Article Feed Description"); feed.setPublishedDate(new Date()); - List list = new ArrayList(); - - SyndEntry item1 = new SyndEntryImpl(); + ArticleItem item1 = new ArticleItem(); item1.setLink("http://www.baeldung.com/netty-exception-handling"); item1.setTitle("Exceptions in Netty"); - SyndContent description1 = new SyndContentImpl(); - description1.setValue("In this quick article, we’ll be looking at exception handling in Netty."); - item1.setDescription(description1); + item1.setDescription("In this quick article, we’ll be looking at exception handling in Netty."); item1.setPublishedDate(new Date()); item1.setAuthor("Carlos"); - SyndEntry item2 = new SyndEntryImpl(); + ArticleItem item2 = new ArticleItem(); item2.setLink("http://www.baeldung.com/cockroachdb-java"); item2.setTitle("Guide to CockroachDB in Java"); - SyndContent description2 = new SyndContentImpl(); - description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java."); - item2.setDescription(description2); + item2.setDescription("This tutorial is an introductory guide to using CockroachDB with Java."); item2.setPublishedDate(new Date()); item2.setAuthor("Baeldung"); - list.add(item1); - list.add(item2); - feed.setEntries(list); + feed.addItem(item1); + feed.addItem(item2); - return new SyndFeedOutput().outputString(feed); + return feed; } } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java new file mode 100644 index 0000000000..258712eb2d --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/RssData.java @@ -0,0 +1,56 @@ +package com.baeldung.spring.controller.rss; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class RssData implements Serializable { + private String link; + private String title; + private String description; + private String publishedDate; + + public String getLink() { + return link; + } + + public void setLink(String link) { + this.link = link; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getPublishedDate() { + return publishedDate; + } + + public void setPublishedDate(Date publishedDate) { + DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); + this.publishedDate = df.format(publishedDate); + } + + @Override + public String toString() { + return "RssData{" + + "link='" + link + '\'' + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + ", publishedDate=" + publishedDate + + '}'; + } +}