【问题标题】:Twitter raw JSON Spring social TwitterTwitter 原始 JSON 春季社交 Twitter
【发布时间】:2016-05-10 03:55:58
【问题描述】:

如何使用 Spring Social Twitter API 获取原始 JSON 数据推文?有“Tweet”类,但我没有找到任何允许检索原始推文内容的函数 - 由 Twitter 以 JSON 格式返回。

【问题讨论】:

    标签: spring-social spring-social-twitter


    【解决方案1】:

    我不知道您为什么想要原始 JSON 数据,但这是可能的,您可以通过以下方式获取它:

    关注Guide 设置 Spring Social Twitter。

    如果您想要来自 Twitter 的原始 JSON 数据,那么您可以使用从 TwitterTemplate 获得的 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();
    
            //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 以获取上述代码。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 2015-01-07
      • 2016-05-04
      • 1970-01-01
      • 2014-09-21
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      相关资源
      最近更新 更多