Twitter API for Java - Hello Twitter Servlet (TOTD #179)


There are a few Twitter APIs for Java that allow you to integrate Twitter functionality in a Java application. This is yet another API, built using JAX-RS and Jersey stack. I started this effort earlier this year and kept delaying to share because wanted to provide a more comprehensive API. But I've delayed enough and releasing it as a work-in-progress.

I'm happy to take contributions in order to evolve this API and make it complete, useful, and robust. Drop a comment on the blog if you are interested or ping me at @arungupta.

How do you get started ? Just add the following to your "pom.xml":

<dependency>
<groupId>org.glassfish.samples</groupId>
<artifactId>twitter-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

The implementation of this API uses Jersey OAuth Filters for authentication with Twitter and so the following dependencies are required if any API that requires authentication, which is pretty much all the APIs ;-)
<dependency>
<groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
    <artifactId>oauth-client</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
    <artifactId>oauth-signature</artifactId>
    <version>${jersey.version}</version>
</dependency> 
Once the dependencies are added to your project, inject Twitter  API in your Servlet (or any other Java EE component) as:

@Inject Twitter twitter;

Here is a simple non-secure invocation of the API to get you started:

SearchResults result = twitter.search("glassfish", SearchResults.class);
for (SearchResultsTweet t : result.getResults()) {
out.println(t.getText() + "<br/>");
}
This code returns the tweets that matches the query "glassfish".

The source code for the complete project can be downloaded here. Download it, unzip, and mvn package will build the .war file. And then deploy it on GlassFish or any other Java EE 6 compliant application server!

The source code for the API also acts as the javadocs and can be checked out from here.

Read README.txt for complete details on how to get started.

A more detailed sample using security and several other API from this library is coming soon!

Comments:

Hi,thanks for the concise tutorial.

I get the error below when running it on glassfish.

[#|2012-06-19T14:54:12.525+0200|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=23;_ThreadName=Thread-2;|StandardWrapperValve[org.javaee.twitterhello.TwitterServlet]: PWC1406: Servlet.service() for servlet org.javaee.twitterhello.TwitterServlet threw exception
com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "recent_retweets" (Class org.glassfish.samples.twitter.api.Metadata), not marked as ignorable
at [Source: java.io.ByteArrayInputStream@7fcf1934; line: 1, column: 568] (through reference chain: org.glassfish.samples.twitter.api.SearchResults["results"]->org.glassfish.samples.twitter.api.SearchResultsTweet["metadata"]->org.glassfish.samples.twitter.api.Metadata["recent_retweets"])

Posted by guest on June 19, 2012 at 06:01 AM PDT #

Seems like the JSON payload has updated since this API was first created. I just updated the API and also added better logging capabilities as well.

Refer to the README.txt at https://svn.java.net/svn/glassfish-samples~svn/trunk/ws/javaee6/twitter-api/README.txt on how to configure logging.

Can you refresh the API and try again ?

Posted by Arun Gupta on June 21, 2012 at 03:17 PM PDT #

Thanks for all the information ,it was very helpful i really like that you are providing information on core and advance java ,being enrolled in
advance and core java http://www.wiziq.com/course/1779-core-and-advance-java-concepts i was looking for such information on advance and core java and your information helped me a lot.

Posted by sarabjeet on September 25, 2012 at 01:14 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today