Retrieving the tweets of a Twitter user

In this recipe, you'll learn how to retrieve the last tweets of a Twitter user from a Spring web application.

Getting ready

This recipe uses the code from the Connecting to Twitter recipe.

How to do it…

Here are the steps to retrieve the last tweets of a Twitter user:

  1. In the TwitterController class, add a Model argument to the tw() method:
    @RequestMapping("/fw")
    public String fb(HttpServletRequest request, Model model) {
    ...
  2. In that method, use the Twitter object to retrieve the user's tweets:
    List<Tweet> tweets = twitter.timelineOperations().getUserTimeline();
  3. Pass the list of tweets to the JSP view:
    model.addAttribute("tweets", tweets);
  4. In the JSP, display the list of tweets:
    <c:forEach items="${tweets}" var="tweet"> ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.