Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Quick Start: REST Integration in NetBeans Platform 6.9

DZone's Guide to

Quick Start: REST Integration in NetBeans Platform 6.9

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In the upcoming release of the NetBeans Platform, already in 6.9 Milestone 1 (as shown here), one big new feature will be native REST support in NetBeans module projects. What does this mean? Whereas in the past you would need to create a REST client in a separate JAR and then attach that JAR within a library wrapper module to your NetBeans Platform application... you will now be able to generate your REST client code from inside a Java source file within a NetBeans module, from this popup (press Alt-Insert to see it already in the daily builds):

Here is a small scenario that shows how all the different parts of the REST story fit together in the upcoming NetBeans Platform 6.9.

  1. Install the Correct Distribution of NetBeans IDE. The REST support in NetBeans IDE is part of the "Java" distribution (or the "All" distribution). While typically it makes sense to use the "Java SE" distribution only for NetBeans Platform application development, since it is smaller, you will now need to use a larger distribution, since only the "Java" (and "All") distributions include the web development tooling, which includes tooling for REST development. Go here to download NetBeans IDE (development builds of NetBeans IDE 6.9 already are able to do everything described below).

  2. Create a REST Service. For demo purposes, just go to the New Project wizard and choose the Hello World JavaEE 6 REST sample, as shown below:

  3. Complete the wizard above and then deploy the simple Hello World REST service that is created for you.

  4. Create a NetBeans Platform Application and a NetBeans Module. Now use the New Project wizard again and create a NetBeans Platform application. Once you have that application, create a new module within that application. Both these steps are done via the New Project wizard's "NetBeans Modules" category.

    Note: You need to make the REST JARs available to your application. They have been wrapped in modules and are available to your NetBeans Platform application, if you choose to make them available. To do so, right-click the project node of the application, choose Properties, and then go to the Libraries panel in the Project Properties dialog. There, expand the "enterprise" node and then choose "RESTful Web Service Libraries". Then click the "Resolve" button so that all the modules that are needed by "RESTful Web Service Libraries" are also made available to the application. Next, right-click the module in the Projects window, choose Properties, go to Libraries, and then set a dependency in your module on "RESTful Web Service Libraries". Now you will be able to use the Jersey APIs, later in this procedure.

  5. Create an Action. Right-click the module's project node in the Projects window, choose New | Other and then choose Module Development | Action. Set any name and choose any categories and complete the wizard. You now have a NetBeans Platform application containing one module, together with one action, consisting of an ActionListener registered in the layer.xml file of the module.

  6. Generate REST Client Code. Inside the Action class (which is simply a plain ActionListener from the JDK, i.e., not some special NetBeans API class), press Alt-Insert. You now see the "Generate Rest Client" menu item shown in the first image above. Choose it and then you'll see this:

    All REST services available in the IDE as projects are shown in the dialog above. In this case, we only have one. Click OK. Your ActionListener now has this content:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public final class SomeAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {


    }

    static class HelloWorldResource_JerseyClient {

    private com.sun.jersey.api.client.WebResource webResource;
    private com.sun.jersey.api.client.Client client;
    private static final String RESOURCE_URI = "http://localhost:8080/HelloWorld/resources/helloWorld";

    HelloWorldResource_JerseyClient() {
    client = new com.sun.jersey.api.client.Client();
    webResource = client.resource(RESOURCE_URI);
    }

    public String getXml() throws com.sun.jersey.api.client.UniformInterfaceException {
    return webResource.accept(javax.ws.rs.core.MediaType.TEXT_HTML).get(String.class);
    }

    public void putXml(Object requestEntity) throws com.sun.jersey.api.client.UniformInterfaceException {
    webResource.type(javax.ws.rs.core.MediaType.TEXT_PLAIN).put(requestEntity);
    }

    public void close() {
    client.destroy();
    }
    }

    }

You're finished, that's all you needed to do to generate the code that will turn your NetBeans Platform application into a REST client.

Conclusion

Now you're ready to use the generated code and run the application. For example, in the "actionPerformed" above, you could put this:

public void actionPerformed(ActionEvent e) {

String message = new HelloWorldResource_JerseyClient().getXml();
JOptionPane.showMessageDialog(null, message);

}

Run the application, invoke the action (based on where you registered it in the layer.xml file) and you will see your message in a JOptionPane:

Congratulations, this simple quick start for REST integration with the NetBeans Platform is complete. A more complex scenario will be presented next time, but now you know everything needed to get started with this new tooling support in NetBeans IDE 6.9 for creating REST clients on the NetBeans Platform.

 

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}