java - Spring Social Twitter Save Data to MySQL -


i'm trying save data request made against twitterapi. jknow, have set objects want in updatetweetsservice class have idea how parse them. have 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 request usertimeline 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      */      public integer getid() {         return id;     }      public void setid(integer id) {         this.id = id;     }      public string getprofileimageurl() {         return profileimageurl;     }      public void setprofileimageurl(string profileimageurl) {         this.profileimageurl = profileimageurl;     }      public string getfromuser() {         return fromuser;     }      public void setfromuser(string fromuser) {         this.fromuser = fromuser;     }      public string gettext() {         return text;     }      public void settext(string text) {         this.text = text;     }      public string geturl() {         return url;     }      public void seturl(string url) {         this.url = url;     }      public float getcreatedate() {         return createdate;     }      public void setcreatedate(float createdate) {         this.createdate = createdate;     }     } 

you can use spring data jpa save entities. easy setup database spring boot.

you can checkout code in github repo, updated this. have used postgresql, can change mysql updating pom.xml , application.properties file per below steps.

you need follow below steps:

  1. add spring-boot-starter-data-jpa , mysql dependencies in 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 database config/properties in 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 entity class tweetentity did, rename tweet else avoid confusion between tweet.class , org.springframework.social.twitter.api.tweet.class

  4. create spring data jpa repositories interface entity.

    package com.rawsanj.tweet.repository;  import org.springframework.data.jpa.repository.jparepository;  import com.rawsanj.tweet.entity.tweetentity;  /**  * spring data jpa repository user entity.  */ public interface tweetzrepository extends jparepository<tweetentity, long> {  } 

    just defining above spring data repository several methods (like save(entity entity), findone(long id), etc) implemented.

  5. let springboot know repository package, i.e. enable jparepositories.

    @springbootapplication @enablejparepositories("com.rawsanj.tweet.repository") public class application { .... .. 
  6. and update controller:

    package com.rawsanj.tweet.controller; .... import com.rawsanj.tweet.entity.tweetentity; import com.rawsanj.tweet.repository.tweetzrepository ;  @controller @requestmapping("/") public class hellocontroller {      private twittertemplate twittertemplate;     private tweetzrepository tweetzrepository ;      @inject     public hellocontroller(streamservice streamservice, twittertemplate twittertemplate, tweetzrepository tweetzrepository ) {         this.streamservice = streamservice;         this.twittertemplate=twittertemplate;         this.tweetzrepository =tweetzrepository ;     }      @requestmapping(value = "tweet/{search}/{count}",method=requestmethod.get)     public string searchtwitter(model model, @pathvariable string search, @pathvariable int count) {         searchresults results = twittertemplate.searchoperations().search(                 new searchparameters(search)                     .resulttype(searchparameters.resulttype.recent)                     .count(count));          list<tweet> tweets = results.gettweets();                 model.addattribute("tweets", tweets);          (tweet tweet : tweets) {                         tweetentity tweetentity = new tweetentity(tweet.gettext(), tweet.getcreatedat(), tweet.getfromuser(), tweet.getlanguagecode(), tweet.getlanguagecode());             tweetzrepository.save(tweetentity);         }         return "search";     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -