2016-05-10 9 views
0

春のソーシャルTwitter APIを使用して独自のJSONデータのツイートを取得するには? "Tweet"クラスがありますが、TwitterでJSON形式で返されたオリジナルのツイートコンテンツを取得する機能はありませんでした。Twitter raw JSON SpringソーシャルTwitter

答えて

0

あなたは生のJSONデータをしたいですか、なぜ私は知らないが、それは可能であり、ここであなたがそれを取得する方法です。

は、セットアップ春社会のTwitterに、このGuideに従ってください。

TwitterTemplateから取得したRestTemplateをTwitterからの生のJSONデータにしたい場合は、

上記のガイドで、このコントローラーを追加します。

@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(); 

     //More Query Options @ https://dev.twitter.com/rest/reference/get/search/tweets  
     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"; 
    } 

} 

を生ツイートjson.htmlを表示するためのテンプレートを追加します。

<!DOCTYPE html> 
<html> 
    <head> 
     <title>JSON Tweets</title> 
    </head> 
    <body> 
     <h3>Hello, <span th:text="${username}">Some User</span>!</h3> 
     <div th:text="${jsonstring}">JSON Tweets</div> 
    </body> 
</html> 

Project完了し、上記のコードのためのCommit最新のを確認してください。

関連する問題