Wednesday, June 4, 2014

spring social example on spring boot or how I stopped worrying and loved autoconfiguration

As of Spring Boot 1.1.0.RC1, autoconfiguration and the starter pom of Spring Social  is added which means that I won't have to add a hundred dependency to my pom and lots of meaningless Spring configuration will be handled for me. Let's see how it works on an example.

 I will implement a web application of two pages. One will show the given user's Twitter timeline and the other user's profile information. Here's my pom:








As you can see, I have my starter-social-twitter dependency which gives me Spring Social and Web capabilities. I'll add jasper and jstl for my jsp pages to work. My repositories part is quite populated due to the milestone repositories.
Now we will add our Service to do Twitter method calls and a Controller for handling the requests. Our Controller is plain and simple:
public class TwitterController {

    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";
If the request comes with "/timeline/username", our controller will get the user timeline and if it comes with "/profile/username" it will get the user profile from TwitterService. Here's our TwitterService:
public class TwitterService {

    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;
We have a Twitter object that'll be created thanks to Spring Boot's autoconfiguration. We just have to provide an app id and app secret key (a.k.a. consumer key and consumer secret) in our application properties and Boot will do the rest. I'm quoting Twitter object explanation from Spring javadoc: "This instance of TwitterTemplate is limited to only performing operations requiring client authorization. For instance, you can use it to search Twitter, but you cannot use it to post a status update. The client credentials given here are used to obtain a client access token via OAuth 2 Client Credentials Grant". If you try to do a status update, you'll get " Authorization is required for the operation, but the API binding was created without authorization". For further Twitter functionality, we would need to provide access token and access token secret keys as well but as far as I know autoconfiguration would not handle these cases yet.
My JSPs:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<img src="${userProfile.profileImageUrl}"/>  
Screen name: ${userProfile.screenName}
Name: ${}
Description: ${userProfile.description}
Location: ${userProfile.location}
Followers: ${userProfile.followersCount}
</body> </html>
As you can see, profile takes the userProfile provided by our Controller and show the basic profile properties. timeline.jsp
<%@ taglib prefix="c" uri="" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <title>Time Line for <c:out value="${twitterUser}" /> TimeLine</title>
    <c:forEach items="${tweets}" var="tweet">
at <c:out value="${tweet.createdAt}"/></li> <br/> </c:forEach> </ul> </body> </html>
Tweets are shown with their text and creation date. My content:
# Config for JSPs
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

# SPRING SOCIAL TWITTER (TwitterAutoConfiguration) someAppId someSecretId
spring.view properties are for the jsp handling. properties can be obtained from Just login there with your twitter account, create your app and get your api keys. Here's the result:

You can check the code at github.