Home Ask Login Register

Developers Planet

Your answer is one click away!

julien February 2016

Spring Social Twitter Save Data to MySQL

I'm trying to save some data from the request I made against TwitterAPI. I jknow, that i have to set the objects I want in the UpdateTweetsService Class but I have now idea how i parse them. This is what I have so far:

UpdateTweetsService.java Class:

import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.Twitter;
import java.util.List;

public class UpdateTweetsService {@Value("${screenName}")
private final Twitter twitter;

public UpdateTweetsService(Twitter twitter) {
    this.twitter = twitter;

 * Performs a Request to get the UserTimeline from Twitter API

 public List<Tweet> tweets() {

        return twitter.timelineOperations().getUserTimeline("${screenName}");    

Tweet.java Class:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

@Table(name = "tweets")
public class Tweet {

    @Column(columnDefinition = "INT unsigned")
    private Integer id;

    @Length(max = 255)
    @Column(columnDefinition = "VARCHAR(255)", length = 255, nullable = false)
    private String profileImageUrl;

    @Length(max = 64)
    @Column(columnDefinition = "VARCHAR(64)", length = 64, nullable = false)
    private String fromUser;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String text;

    @Length(max = 255)
    @Column(columnDefinition = "VARCHAR(255)", length = 255, nullable = false)
    private String url;

    @Column(columnDefinition = "FLOAT")
    private Float createDate;

     * Getter & Setter        


Sanjay Rawat February 2016

You can use Spring Data JPA to save your entities. Its very easy to setup your database with Spring Boot.

You can checkout the code in my Github repo, I updated it for this. I have used PostgreSql, you can easily change it to MySql by updating pom.xml and application.properties file as per below steps.

You need to follow below steps:

  1. Add spring-boot-starter-data-jpa and mysql dependencies in your pom.xml

  2. Set the database config/properties in your application.properties

  3. Create an Entity class TweetEntity which you already did, but rename it from Tweet to something else to avoid confusion between your Tweet.class and org.springframework.social.twitter.api.Tweet.class

  4. Create Spring Data JPA Repositories interface for your entity.

    package com.rawsanj.tweet.repository;
    import org.springfram 

Post Status

Asked in February 2016
Viewed 2,668 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved