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;

@Inject
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;

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


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

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

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

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

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

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

    /*
     * Getter & Setter        

Answers


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

    .....
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    
  2. Set the database config/properties in your application.properties

    .....
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
    spring.jpa.show-sql=true
    spring.jpa.database=mysql       
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
    spring.datasource.url=jdbc:mysql://localhost:3306/sakila
    spring.datasource.name=sakila
    spring.datasource.username=mysql
    spring.datasource.password=password
    
  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

Search




Leave an answer