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
*/
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 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 toMySqlby updatingpom.xmlandapplication.propertiesfile as per below steps.You need to follow below steps:
Add
spring-boot-starter-data-jpaandmysqldependencies in your pom.xmlSet the database config/properties in your
application.propertiesCreate an Entity class
TweetEntitywhich you already did, but rename it fromTweetto something else to avoid confusion between yourTweet.classandorg.springframework.social.twitter.api.Tweet.classCreate Spring Data JPA Repositories interface for your entity.
Just be defining above Spring Data Repository you get several methods (like
save(Entity entity),findOne(Long Id), etc) already implemented.Let SpringBoot know your repository package, i.e. enable Jparepositories.
And finally update your
Controller: