做微商如何网站推广,北京网站建设方案哪家好,域名信息查询网站,深圳微信小程序制作公司对于Spring Boot 1.1.0.RC1#xff0c;添加了自动配置和Spring Social的启动程序pom#xff0c;这意味着我不必为pom添加一百个依赖关系#xff0c;并且将为我处理许多毫无意义的Spring配置。 让我们来看一个例子。 我将实现一个两页的Web应用程序。 一个将显示给定用户的T… 对于Spring Boot 1.1.0.RC1添加了自动配置和Spring Social的启动程序pom这意味着我不必为pom添加一百个依赖关系并且将为我处理许多毫无意义的Spring配置。 让我们来看一个例子。 我将实现一个两页的Web应用程序。 一个将显示给定用户的Twitter时间轴另一个将显示用户的个人资料信息。 这是我的pom project xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://maven.apache.org/POM/4.0.0 xsi:schemalocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelversion4.0.0/modelversiongroupidnr.co.caught/groupidartifactidBootTwitterJoy/artifactidversion1.0-SNAPSHOT/versionpackagingwar/packaging!-- Inherit defaults from Spring Boot --parentgroupidorg.springframework.boot/groupidartifactidspring-boot-starter-parent/artifactidversion1.1.0.RC1/version/parentdependenciesdependencygroupidorg.springframework.boot/groupidartifactidspring-boot-starter-social-twitter/artifactid/dependency!-- Both are needed for jsp support--dependencygroupidorg.apache.tomcat.embed/groupidartifactidtomcat-embed-jasper/artifactid/dependencydependencygroupidjavax.servlet/groupidartifactidjstl/artifactid/dependency/dependencies!-- Needed for fat jar --buildpluginsplugingroupidorg.springframework.boot/groupidartifactidspring-boot-maven-plugin/artifactid/plugin/plugins/build!-- Add Spring repositories --!-- (you dont need this if you are using a .RELEASE version) --repositoriesrepositoryidspring-snapshots/idurlhttp://repo.spring.io/snapshot/urlsnapshotsenabledtrue/enabled/snapshots/repositoryrepositoryidspring-milestones/idurlhttp://repo.spring.io/milestone/url/repository/repositoriespluginrepositoriespluginrepositoryidspring-snapshots/idurlhttp://repo.spring.io/snapshot/url/pluginrepositorypluginrepositoryidspring-milestones/idurlhttp://repo.spring.io/milestone/url/pluginrepository/pluginrepositories/project 如您所见我具有starter-social-twitter依赖关系该依赖关系为我提供了Spring Social和Web功能。 我将为我的jsp页面添加jasper和jstl。 由于具有里程碑意义的存储库因此我的存储库部分已足够填充。 现在我们将添加我们的服务来执行Twitter方法调用和一个用于处理请求的控制器。 我们的控制器简单明了 Controller
public class TwitterController {Autowiredprivate 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 {Autowiredprivate 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 contentTypetext/html;charsetUTF-8 languagejava %
html
headtitle/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 prefixc urihttp://java.sun.com/jsp/jstl/core %
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headtitleTime Line for c:out value${twitterUser} / TimeLine/title
/head
body
ulc:forEach items${tweets} vartweetli${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上检查代码。 翻译自: https://www.javacodegeeks.com/2014/06/spring-social-example-on-spring-boot-or-how-i-stopped-worrying-and-loved-autoconfiguration.html