Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…json-spring-social-twitter

This is a demo commit on how to use RestTemplate to get raw json Tweets from Twitter
  • Loading branch information
RawSanj committed May 10, 2016
1 parent 2b44680 commit 0e94f05
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
@@ -0,0 +1,52 @@
package com.rawsanj.tweet.controller;

import javax.inject.Inject;

import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UserProfile;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

@Controller
@RequestMapping("/jsontweets")
public class JsonTweetsController {

private ConnectionRepository connectionRepository;

private TwitterTemplate twitterTemplate;

@Inject
public JsonTweetsController(Twitter twitter, ConnectionRepository connectionRepository, TwitterTemplate twitterTemplate) {
this.connectionRepository = connectionRepository;
this.twitterTemplate = twitterTemplate;
}

@RequestMapping(method=RequestMethod.GET)
public String helloTwitter(@RequestParam String search, Model model) {
if (connectionRepository.findPrimaryConnection(Twitter.class) == null) {
return "redirect:/connect/twitter";
}

Connection<Twitter> con = connectionRepository.findPrimaryConnection(Twitter.class);
UserProfile userProfile = con.fetchUserProfile();
String username = userProfile.getFirstName() + " " + userProfile.getLastName();

RestTemplate restTemplate = twitterTemplate.getRestTemplate();

String response = restTemplate.getForObject("https://api.twitter.com/1.1/search/tweets.json?q="+search, String.class);
System.out.println("JSON Response From Twitter: "+response);

model.addAttribute("jsonstring", response);
model.addAttribute("username", username);

return "json";
}

}
Expand Up @@ -20,16 +20,15 @@
@Controller
@RequestMapping("/userinfo")
public class UserInfoController {

private Twitter twitter;

private ConnectionRepository connectionRepository;

private TwitterTemplate twitterTemplate;

@Inject
public UserInfoController(Twitter twitter, ConnectionRepository connectionRepository, TwitterTemplate twitterTemplate) {
this.twitter = twitter;
this.connectionRepository = connectionRepository;
this.twitterTemplate = twitterTemplate;
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/resources/templates/json.html
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>JSON Tweets</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
</head>
<body>
<div class="container">


<h3>Hello, <span th:text="${username}">Some User</span>!</h3>

<div th:text="${jsonstring}">
JSON Tweets
</div>


</div>
<!-- Latest compiled and minified jQuery JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<!-- Latest compiled and minified Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

0 comments on commit 0e94f05

Please sign in to comment.