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

Guest Author


src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/e4604a9f59a9aa375acc367cf24f672e/twitter_bird_callout.png"> There are a few href="https://dev.twitter.com/docs/twitter-libraries#java">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

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 href="http://twitter.com/arungupta">@arungupta.

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


The implementation of this API uses href="https://wikis.oracle.com/display/Jersey/OAuth">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 ;-)

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

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 href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/66659ba9288417585e5852de4fcf6e38/totd178_twitter_hello.zip">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 href="https://svn.java.net/svn/glassfish-samples%7Esvn/trunk/ws/javaee6/twitter-api/">from

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!

Join the discussion

Comments ( 3 )
  • guest Tuesday, June 19, 2012

    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"])

  • Arun Gupta Thursday, June 21, 2012

    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 ?

  • sarabjeet Tuesday, September 25, 2012

    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.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.