Skip to content

Commit 1b0e859

Browse files
Dassi orleandomaibin
authored andcommitted
BAEL-1273: RSS improvements with a custom model (eugenp#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
1 parent 2a127b3 commit 1b0e859

6 files changed

Lines changed: 169 additions & 23 deletions

File tree

‎spring-mvc-simple/pom.xml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@
3131
<junit.jupiter.version>5.0.2</junit.jupiter.version>
3232
<junit.platform.version>1.0.2</junit.platform.version>
3333
<rome.version>1.9.0</rome.version>
34+
<jackson.version>2.9.4</jackson.version>
35+
<xstream.version>1.4.9</xstream.version>
3436
</properties>
3537

3638
<dependencies>
39+
<dependency>
40+
<groupId>org.springframework</groupId>
41+
<artifactId>spring-oxm</artifactId>
42+
<version>5.0.2.RELEASE</version>
43+
</dependency>
3744
<dependency>
3845
<groupId>javax.servlet</groupId>
3946
<artifactId>javax.servlet-api</artifactId>
@@ -121,6 +128,23 @@
121128
<artifactId>rome</artifactId>
122129
<version>${rome.version}</version>
123130
</dependency>
131+
132+
<dependency>
133+
<groupId>com.fasterxml.jackson.core</groupId>
134+
<artifactId>jackson-databind</artifactId>
135+
<version>${jackson.version}</version>
136+
</dependency>
137+
<dependency>
138+
<groupId>com.fasterxml.jackson.dataformat</groupId>
139+
<artifactId>jackson-dataformat-xml</artifactId>
140+
<version>${jackson.version}</version>
141+
</dependency>
142+
143+
<dependency>
144+
<groupId>com.thoughtworks.xstream</groupId>
145+
<artifactId>xstream</artifactId>
146+
<version>${xstream.version}</version>
147+
</dependency>
124148
</dependencies>
125149
<build>
126150
<pluginManagement>

‎spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
package com.baeldung.spring.configuration;
22

33
import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver;
4+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
5+
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
46
import org.springframework.context.annotation.Bean;
57
import org.springframework.context.annotation.ComponentScan;
68
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.http.converter.HttpMessageConverter;
10+
import org.springframework.http.converter.feed.RssChannelHttpMessageConverter;
11+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
12+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
13+
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
14+
import org.springframework.web.accept.ContentNegotiationManager;
715
import org.springframework.web.multipart.MultipartResolver;
816
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
917
import org.springframework.web.servlet.ViewResolver;
1018
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
1119
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
12-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
20+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
1321
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
1422
import org.springframework.web.servlet.view.InternalResourceViewResolver;
15-
import org.springframework.web.accept.ContentNegotiationManager;
1623

17-
import java.util.List;
1824
import java.util.ArrayList;
25+
import java.util.List;
1926

2027
@Configuration
2128
@EnableWebMvc
2229
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
23-
class ApplicationConfiguration implements WebMvcConfigurer {
30+
class ApplicationConfiguration extends WebMvcConfigurerAdapter {
2431

2532
@Override
2633
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
@@ -49,4 +56,19 @@ public MultipartResolver multipartResolver() {
4956
multipartResolver.setMaxUploadSize(5242880);
5057
return multipartResolver;
5158
}
59+
60+
@Override
61+
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
62+
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
63+
builder.indentOutput(true);
64+
65+
XmlMapper xmlMapper = builder.createXmlMapper(true).build();
66+
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
67+
68+
converters.add(new RssChannelHttpMessageConverter());
69+
converters.add(new MappingJackson2HttpMessageConverter());
70+
converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper));
71+
72+
super.configureMessageConverters(converters);
73+
}
5274
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.spring.controller.rss;
2+
3+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
4+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
5+
6+
import java.io.Serializable;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
@JacksonXmlRootElement(localName="articles")
11+
public class ArticleFeed extends RssData implements Serializable {
12+
13+
@JacksonXmlElementWrapper(localName = "items", useWrapping = true)
14+
private List<ArticleItem> items = new ArrayList<ArticleItem>();
15+
16+
public void addItem(ArticleItem articleItem) {
17+
this.items.add(articleItem);
18+
}
19+
20+
public List<ArticleItem> getItems() {
21+
return items;
22+
}
23+
24+
public void setItems(List<ArticleItem> items) {
25+
this.items = items;
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.spring.controller.rss;
2+
3+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
4+
5+
import java.io.Serializable;
6+
7+
@JacksonXmlRootElement(localName="article")
8+
public class ArticleItem extends RssData implements Serializable {
9+
private String author;
10+
11+
public String getAuthor() {
12+
return author;
13+
}
14+
15+
public void setAuthor(String author) {
16+
this.author = author;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "ArticleItem{" +
22+
"author='" + author + '\'' +
23+
'}';
24+
}
25+
}

‎spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,38 @@
1414
@Controller
1515
public class ArticleRssController {
1616

17-
@GetMapping(value = "/rssMvc")
17+
@GetMapping(value = "/rss1")
1818
public String articleMvcFeed() {
1919
return "articleFeedView";
2020
}
2121

22-
@GetMapping(value = "/rssRest", produces = "application/rss+xml")
22+
@GetMapping(value = "/rss2")
2323
@ResponseBody
24-
public String articleRestFeed() throws FeedException {
25-
SyndFeed feed = new SyndFeedImpl();
26-
feed.setFeedType("rss_2.0");
24+
public ArticleFeed articleRestFeed2() {
25+
ArticleFeed feed = new ArticleFeed();
2726
feed.setLink("http://localhost:8080/spring-mvc-simple/rss");
2827
feed.setTitle("Article Feed");
2928
feed.setDescription("Article Feed Description");
3029
feed.setPublishedDate(new Date());
3130

32-
List list = new ArrayList<SyndEntry>();
33-
34-
SyndEntry item1 = new SyndEntryImpl();
31+
ArticleItem item1 = new ArticleItem();
3532
item1.setLink("http://www.baeldung.com/netty-exception-handling");
3633
item1.setTitle("Exceptions in Netty");
37-
SyndContent description1 = new SyndContentImpl();
38-
description1.setValue("In this quick article, we’ll be looking at exception handling in Netty.");
39-
item1.setDescription(description1);
34+
item1.setDescription("In this quick article, we’ll be looking at exception handling in Netty.");
4035
item1.setPublishedDate(new Date());
4136
item1.setAuthor("Carlos");
4237

43-
SyndEntry item2 = new SyndEntryImpl();
38+
ArticleItem item2 = new ArticleItem();
4439
item2.setLink("http://www.baeldung.com/cockroachdb-java");
4540
item2.setTitle("Guide to CockroachDB in Java");
46-
SyndContent description2 = new SyndContentImpl();
47-
description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java.");
48-
item2.setDescription(description2);
41+
item2.setDescription("This tutorial is an introductory guide to using CockroachDB with Java.");
4942
item2.setPublishedDate(new Date());
5043
item2.setAuthor("Baeldung");
5144

52-
list.add(item1);
53-
list.add(item2);
54-
feed.setEntries(list);
45+
feed.addItem(item1);
46+
feed.addItem(item2);
5547

56-
return new SyndFeedOutput().outputString(feed);
48+
return feed;
5749
}
5850

5951
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.spring.controller.rss;
2+
3+
import java.io.Serializable;
4+
import java.text.DateFormat;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
8+
public class RssData implements Serializable {
9+
private String link;
10+
private String title;
11+
private String description;
12+
private String publishedDate;
13+
14+
public String getLink() {
15+
return link;
16+
}
17+
18+
public void setLink(String link) {
19+
this.link = link;
20+
}
21+
22+
public String getTitle() {
23+
return title;
24+
}
25+
26+
public void setTitle(String title) {
27+
this.title = title;
28+
}
29+
30+
public String getDescription() {
31+
return description;
32+
}
33+
34+
public void setDescription(String description) {
35+
this.description = description;
36+
}
37+
38+
public String getPublishedDate() {
39+
return publishedDate;
40+
}
41+
42+
public void setPublishedDate(Date publishedDate) {
43+
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
44+
this.publishedDate = df.format(publishedDate);
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "RssData{" +
50+
"link='" + link + '\'' +
51+
", title='" + title + '\'' +
52+
", description='" + description + '\'' +
53+
", publishedDate=" + publishedDate +
54+
'}';
55+
}
56+
}

0 commit comments

Comments
 (0)