Improve rss(BAEL-1273) and done with BAEL-1519 (#3692)
* 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 * BAEL-1519: Guide to scribejava * BAEL-1273: improve xml representation * Fix pom
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
<rome.version>1.9.0</rome.version>
|
||||
<jackson.version>2.9.4</jackson.version>
|
||||
<xstream.version>1.4.9</xstream.version>
|
||||
<scribejava.version>5.1.0</scribejava.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -145,6 +146,11 @@
|
||||
<artifactId>xstream</artifactId>
|
||||
<version>${xstream.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.scribejava</groupId>
|
||||
<artifactId>scribejava-apis</artifactId>
|
||||
<version>${scribejava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
|
||||
@@ -7,6 +7,7 @@ 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.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.feed.RssChannelHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
@@ -65,6 +66,7 @@ class ApplicationConfiguration extends WebMvcConfigurerAdapter {
|
||||
XmlMapper xmlMapper = builder.createXmlMapper(true).build();
|
||||
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
|
||||
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
converters.add(new RssChannelHttpMessageConverter());
|
||||
converters.add(new MappingJackson2HttpMessageConverter());
|
||||
converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.spring.controller.rss;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
|
||||
@@ -10,7 +11,8 @@ import java.util.List;
|
||||
@JacksonXmlRootElement(localName="articles")
|
||||
public class ArticleFeed extends RssData implements Serializable {
|
||||
|
||||
@JacksonXmlElementWrapper(localName = "items", useWrapping = true)
|
||||
@JacksonXmlProperty(localName = "item")
|
||||
@JacksonXmlElementWrapper(useWrapping = false)
|
||||
private List<ArticleItem> items = new ArrayList<ArticleItem>();
|
||||
|
||||
public void addItem(ArticleItem articleItem) {
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
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;
|
||||
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
package com.baeldung.spring.controller.rss;
|
||||
|
||||
import com.rometools.rome.feed.synd.*;
|
||||
import com.rometools.rome.io.FeedException;
|
||||
import com.rometools.rome.io.SyndFeedOutput;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class ArticleRssController {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.spring.controller.scribe;
|
||||
|
||||
import com.github.scribejava.apis.TwitterApi;
|
||||
import com.github.scribejava.core.builder.ServiceBuilder;
|
||||
import com.github.scribejava.core.model.*;
|
||||
import com.github.scribejava.core.oauth.OAuth10aService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("twitter")
|
||||
public class ScribeController {
|
||||
|
||||
private OAuth10aService createTwitterService() {
|
||||
return new ServiceBuilder("PSRszoHhRDVhyo2RIkThEbWko")
|
||||
.apiSecret("prpJbz03DcGRN46sb4ucdSYtVxG8unUKhcnu3an5ItXbEOuenL")
|
||||
.callback("http://localhost:8080/spring-mvc-simple/twitter/callback")
|
||||
.build(TwitterApi.instance());
|
||||
}
|
||||
|
||||
@GetMapping(value = "/authorization")
|
||||
public RedirectView authorization(HttpServletRequest servletReq) throws InterruptedException, ExecutionException, IOException {
|
||||
OAuth10aService twitterService = createTwitterService();
|
||||
|
||||
OAuth1RequestToken requestToken = twitterService.getRequestToken();
|
||||
String authorizationUrl = twitterService.getAuthorizationUrl(requestToken);
|
||||
servletReq.getSession().setAttribute("requestToken", requestToken);
|
||||
|
||||
RedirectView redirectView = new RedirectView();
|
||||
redirectView.setUrl(authorizationUrl);
|
||||
return redirectView;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/callback", produces = "text/plain")
|
||||
@ResponseBody
|
||||
public String callback(HttpServletRequest servletReq, @RequestParam("oauth_verifier") String oauthV) throws InterruptedException, ExecutionException, IOException {
|
||||
OAuth10aService twitterService = createTwitterService();
|
||||
OAuth1RequestToken requestToken = (OAuth1RequestToken) servletReq.getSession().getAttribute("requestToken");
|
||||
OAuth1AccessToken accessToken = twitterService.getAccessToken(requestToken, oauthV);
|
||||
|
||||
OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json");
|
||||
twitterService.signRequest(accessToken, request);
|
||||
Response response = twitterService.execute(request);
|
||||
|
||||
return response.getBody();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user