Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/com/rawsanj/tweet/controller/JsonTweetsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |