对于Spring Boot 1.1.0.RC1,添加了自动配置和Spring Social的启动程序pom,这意味着我不必为pom添加一百个依赖关系,并且将为我处理许多毫无意义的Spring配置。 让我们来看一个例子。
我将实现一个两页的Web应用程序。 一个将显示给定用户的Twitter时间轴,另一个将显示用户的个人资料信息。 这是我的pom:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
<groupid>nr.co.caught</groupid>
<artifactid>BootTwitterJoy</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>1.1.0.RC1</version>
</parent>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-social-twitter</artifactid>
</dependency>
<!-- Both are needed for jsp support-->
<dependency>
<groupid>org.apache.tomcat.embed</groupid>
<artifactid>tomcat-embed-jasper</artifactid>
</dependency>
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>jstl</artifactid>
</dependency>
</dependencies>
<!-- Needed for fat jar -->
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
</build>
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginrepositories>
<pluginrepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginrepository>
<pluginrepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginrepository>
</pluginrepositories>
</project>
如您所见,我具有starter-social-twitter依赖关系,该依赖关系为我提供了Spring Social和Web功能。 我将为我的jsp页面添加jasper和jstl。 由于具有里程碑意义的存储库,因此我的存储库部分已足够填充。
现在,我们将添加我们的服务来执行Twitter方法调用和一个用于处理请求的控制器。 我们的控制器简单明了:
@Controller
public class TwitterController {
@Autowired
private TwitterService twitterService;
@RequestMapping(value = "/timeline/{twitterUser}")
public String getUserTimeline(@PathVariable String twitterUser, Model model) {
model.addAttribute("tweets", twitterService.getUserTimeline(twitterUser));
model.addAttribute("user", twitterUser);
return "timeline";
}
@RequestMapping(value = "/profile/{twitterUser}")
public String getUserProfile(@PathVariable String twitterUser, Model model) {
model.addAttribute("userProfile", twitterService.getUserProfile(twitterUser));
return "profile";
}
}
如果请求带有“ / timeline / username”,我们的控制器将获取用户时间轴,如果带有“ / profile / username”,它将从TwitterService获取用户配置文件。 这是我们的TwitterService:
@Service
public class TwitterService {
@Autowired
private Twitter twitter;
public List < Tweet > getUserTimeline(String twitterUser) {
TimelineOperations timelineOps = twitter.timelineOperations();
List tweets = timelineOps.getUserTimeline("@" + twitterUser);
return tweets;
}
public TwitterProfile getUserProfile(String twitterUser) {
UserOperations userOperations = twitter.userOperations();
TwitterProfile userProfile = userOperations.getUserProfile(twitterUser);
return userProfile;
}
}
由于Spring Boot的自动配置,我们将创建一个Twitter对象。 我们只需要在我们的应用程序属性中提供一个应用程序ID和应用程序秘密密钥(又名“消费者密钥”和“消费者秘密”),Boot将完成其余的工作。 我引用了Spring javadoc中的Twitter对象说明:
“ TwitterTemplate的此实例仅限于执行需要客户端授权的操作。 例如,您可以使用它来搜索Twitter,但不能使用它来发布状态更新。 此处提供的客户端凭据用于通过OAuth 2客户端凭据授予获取客户端访问令牌。
如果您尝试进行状态更新,则会得到“ org.springframework.social.MissingAuthorizationException:操作需要授权,但API绑定是未经授权创建的”。 对于进一步的Twitter功能,我们还需要提供访问令牌和访问令牌秘密密钥,但据我所知,自动配置尚无法解决这些情况。
我的JSP:
profile.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<img src="${userProfile.profileImageUrl}"/>
Screen name: ${userProfile.screenName}
Name: ${userProfile.name}
Description: ${userProfile.description}
Location: ${userProfile.location}
Followers: ${userProfile.followersCount}
</body>
</html>
如您所见,概要文件采用了我们控制器提供的userProfile并显示了基本概要文件属性。 timeline.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Time Line for <c:out value="${twitterUser}" /> TimeLine</title>
</head>
<body>
<ul>
<c:forEach items="${tweets}" var="tweet">
<li>${tweet.text}
at <c:out value="${tweet.createdAt}"/></li>
</c:forEach>
</ul>
</body>
</html>
显示推文及其文本和创建日期。 我的application.properties内容:
# Config for JSPs
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
# SPRING SOCIAL TWITTER (TwitterAutoConfiguration)
spring.social.twitter.appId= someAppId
spring.social.twitter.appSecret= someSecretId
spring.view属性用于jsp处理。 spring.social.twitter属性,可以从http://dev.twitter.com获得 。 只需使用您的Twitter帐户登录那里,创建您的应用并获取api密钥。 结果如下:
- 您可以在github上检查代码。