Creating RESTful Service Clients in NetBeans Modules

Starting in NetBeans IDE 6.9, native REST support is available in NetBeans Module projects. You can now directly generate RESTful client code in a NetBeans module. You can also insert Jersey RESTful client code in a Java or Java Web application.

In this tutorial, you create a NetBeans platform application that consumes the Twitter What Are You Doing service, displaying a list of your Twitter friends' status messages. First you create the platform application. You select the libraries needed in the application. Then you create a NetBeans module. Finally, you add a RESTful client and some basic display elements to the module. The client uses OAuth authorization.

Note: To use the procedure described in this tutorial with a Java or Java Web project, skip the section on Creating a Platform Application and create a Java or Java Web application instead of a NetBeans Module. Ignore any directions that are specific to NetBeans modules. If you are creating a Java application, you can use most of the design in Designing the Window in a JFrame + JPanel. If you are using a Java Web application, you have to design your own JSP or JSF.

Note: You can follow the procedure in this tutorial with another RESTful web service than the Twitter service, if you have the URL for that service's WADL. You only need to register the service in the IDE. To register a service, open the Services window, right-click the Web Services node, and select Add Web Service.


Content on this page applies to NetBeans IDE 6.9-7.1

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE Java EE download bundle
Java Development Kit (JDK) version 6 or version 7

Creating the Platform Application

You can add NetBeans IDE libraries to a NetBeans Platform Application. In this section, you create the platform application and add the necessary libraries.


To create the module suite:

  1. Click New Project (Ctrl-Shift-N on Linux and Windows, ⌘-Shift-N on MacOS). The New Project wizard opens.
  2. Select the NetBeans Modules category. Then select the NetBeans Platform Application project. Click Next.
  3. Name the project RestfulClientPlatformApp. Choose a location for the project. Accept the other default settings and click Finish. The RestfulClientPlatformApp project appears in the Projects window.
  4. In the Projects window, right-click the RestfulClientPlatformApp project node and select Properties. The Properties dialog opens.
  5. In the Properties dialog, select the Libraries category. Note that only the Platform libraries are included.
  6. Expand the node for the Enterprise libraries. Tick the Included box for the RESTful Web Service Libraries.
    Suite properties showing RESTful WS Libraries selected for inclusion
  7. The Resolve button is highlighted in red, because the RESTful Web Service Libraries depend on other libraries that are not included in the suite. Click the Resolve button to include those libraries.
  8. Click OK. The platform application is ready for you to create the client module.

Creating the Client Module

In just a few steps, you create a module that serves as a client for the Twitter What Are You Doing service. Twitter services are registered "out of the box" in the NetBeans IDE web service manager. You can add additional

To create the module and the client functionality:

  1. In the Projects window, right-click the Modules subnode of the RestfulClientPlatformApp and select Add New... The New Module Project wizard opens.
  2. Name the module TwitterClientModule. Accept the default settings in the other fields and click Next. The Basic Module Configuration panel opens.
  3. Give the code name base an arbitrary name, such as Accept the default settings in the other fields and click Finish. TwitterClientModule now appears in the Projects window, under the Modules node of the platform application.
  4. Right-click the TwitterClientModule node and select Open Project. A TwitterClientModule node now appears at the root level of the Projects window.
    Projects window showing TwitterClientModule root node
  5. Select the new, root-level TwitterClientModule node. Launch the New File wizard (Ctrl-N/⌘-N, or New File icon, or context menu of the node).
  6. In the New File wizard, select the Web Services category and the RESTful Java Client file type. Click Next. The New RESTful Java Client panel opens.
  7. Name the class TwitterClient and give it an arbitrary package name, or select the code name base you previously created.
    New RESTful Client wizard showing class and package name
  8. Under Select the REST resource, select IDE Registered. Click Browse and browse for Twitter > Twitter OAuth > [statuses] > [friends_timeline.{format}]. Select this resource and click OK.
    The friends_timeline rest resource selected in the Available REST Resources dialog

    Note: You can register additional web services in the IDE. Go to the Services window, right-click the Web Services node, and choose Add Web Service. You can add a local file or a RESTful URL.
    Add Web Service option for Web Services manager, Services window

  9. OAuth is automatically selected as the authentication type. Accept all defaults and click Finish.
    Completed new RESTful Client wizard
  10. A warning dialog opens. The dialog asks if you want to generate java artifacts from XML schema references in the WADL file. Click Yes.
  11. Another warning may appear asking you to add modules to the classpath. Click OK.
    Warning dialog about missing dependencies
  12. If you need to add modules to the classpath, right-click the TwitterClientModule node and open its Project Properties. Go to the Libraries section, and add the modules with the Add Dependency button. This button opens a list of module dependencies to browse.
    Twitter Client Module properties window, Libraries section

The TwitterClient class is generated and opens in the editor. TheTwitterClient class is complex and contains the following fields, methods and inner classes:

  • CONSUMER_KEY: Consumer Key string
  • CONSUMER_SECRET: Consumer Sectret string
  • initOAuth(): method for OAuh intitialization
  • getFriendsTimeline(): method corresponding to HTTP method: getFriendsTimeline (from the REST resource) 
  • makeOAuthRequestUnique(): useful for multiple API calls in one session
  • login: used to login to the Twitter Application (forces the authorization). This method calls two more generated methods, getOAuthAccessToken and getOAuthRequestToken.

Next you get OAuth keys from Twitter and add them to TwitterClient.

Getting OAuth Keys from Twitter

In order for the NetBeans Platform application to access Twitter data, you need to get CUSTOMER and CUSTOMER_SECRET keys, along with a verification string, from Twitter. Twitter uses OAuth authorization, which requires these keys. However, OAuth is set up with the expectation that it will be called by a web application on a server. In order to get the authorization keys, you register a "dummy" web application.

To get the OAuth keys from Twitter:

  1. Open a browser. Go to the Twitter > Applications page and click Register a new application . You need to be logged into a Twitter account. Make sure you are logged into the correct account, if you have multiple accounts.
  2. Type NB Platform Friends Application in the Application Name text field.
  3. Type a description into the Description field. This is required. You can type something like "NetBeans Platform application calling the friends_timeline operation."
  4. Type an arbitrary URL into the Application Website field.
  5. In the Application Type option, select the Client radio button.
  6. In the Default Access Type option, select the Read and Write radio button.
  7. Leave other options default and press Save. A browser page opens with the details of the application you registered. The important details are the Consumer key and Consumer secret.
  8. Copy the Consumer key from the browser. In the IDE, locate the line where CONSUMER_KEY is set. Paste the value of the consumer key between the quotation marks.
    TwitterClient showing CONSUMER_KEY and CONSUMER_SECRET location
  9. Copy and paste the Consumer secret key from the browser into TwitterClient. Save your changes.

Designing the Window

To complete the project, add a window. Populate the window with UI components and add actions so that clicking a button, for example, will show a list of friends' statuses.

To design the window:

  1. In the Projects window, right-click the module's node and select New > Window. The New Window wizard opens with the Basic Settings panel.
  2. In the Basic Settings panel, select the editor window position, select Open on Application Start, and click Next. The Name, Icon and Location panel opens.
  3. In the Class Name Prefix field, type twitterFriendsStatus. Select the package. Click Finish.
    Name, Icon and Location panel of New Window wizard, showing class name prefix and package name
  4. The twitterFriendsStatusTopComponent file opens in the Design view. A palette of Swing UI components opens on the right.
    Design view of new window and Palette of swing components, with no components added yet
  5. Drag the following GUI elements into the design area:
    Component Display text Settings
    Button Get Friends' Statuses

    Change variable name to getStatusesButton.

    Unselect "enabled" property

    Unselected 'enabled' property

    Button Log in Change variable name to loginButton
    Scroll pane --  
    Text area -- Drag into scroll pane

    Resize the text area/scroll pane and align the buttons as you like.
    Design view of new window showing completed window

  6. Double-click the Login button. The IDE generates an action method for the button, and the editor switches to the Source view with the action method focused.
  7. Type or copy the following code into the body of the login button action method. This code launches the method for logging the application into Twitter, enables the getStatuses button, and disables the login button. The application only needs to log in once. Note that TwitterClient does not need to be instantiated to call its login method.
    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                   
  8. Double-click the getStatuses button. The IDE generates an action method for the button, and the editor switches to the Source view with the action method focused.
  9. Paste or type the following handling code into the body of the getStatuses button action method. This code instantiates the TwitterClient and initializes OAuth, using the tokens that the login method created. The code then calls getFriendsTimeline, gets a list of Statuses, adds a line with the username and text for each status to a String, and passes the String to the text area.
    private void getStatusesButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    TwitterClient client = new TwitterClient("xml");
    Statuses response = client.getFriendsTimeline(Statuses.class, null, null, null, "10");
    String text = "";
    for (StatusType st : response.getStatus()) {
    text += st.getUser().getName() + ": " + st.getText() + "\n";
  10. The code has some warning icons for classes that aren't found. Press Ctrl-Shift-I (⌘-Shift-I on MacOS). The Fix All Imports dialog opens. Select the twitter.twitteroauth.twitterresponse classes. Click OK.
    Fix All Imports dialog showing the correct classes to import

The application is now complete. Run the RestfulClientPlatformApp, and a NetBeans platform appears with your designed window in the Output section. Click Log In, and a dialog opens with a link to click for you to authorize the application to access data.

Dialog for authorizing OAuth to pass data to application

Click the link, and a browser page opens with Twitter asking if you want to allow your application to access Twitter data. Click Allow, and the page refreshes with a PIN. Copy the PIN and paste it into the authentication dialog's verifier string field. Click OK.

The Log In button is now disabled, and the Get Friends' Statuses button is enabled. Click Get Friends' Statuses , and a list of your Twitter friends' latest status messages appears.

Running application showing status messages

More Exercises

Here are a few more ideas for you to explore:

  • Add another window to the module, using other methods in the friends_timeline API.
  • Add another module to the project, using a different Twitter resource.
  • Explore the Facebook Module Sample at New Project > Samples > NetBeans Modules > Facebook Module Sample

See Also

For more information about using NetBeans IDE to develop Java EE applications, see the following resources:

To send comments and suggestions, get support, and keep informed about the latest developments on the NetBeans IDE Java EE development features, join the mailing list.

To send comments and suggestions, get support, and keep informed on topics connected with developing RCP applications on the NetBeans platform, join the mailing list.

get support for the NetBeans