Monday September 03, 2007
Java Clients and Web Services in NetBeans IDE 6.0
I've seen several questions on mailing lists about (1) how to use a web service in a Java application and (2) how to create a NetBeans module that uses a web service. Both in relation to (the forthcoming) NetBeans IDE 6.0, running on JDK 6. The two scenarios are related, the second is an extension of the first. So let's go through the whole process. At the end, we'll have a new menu in the IDE which, when you click it, shows a "thought for the day" from this on-line web service in the Output window:
The main thing to notice, while you follow the steps below, is that there's surprisingly little coding involved to create the above new NetBeans "thought for the day" feature. The second thing to notice is that there are LOTS of tools in the IDE to help you to create this feature. And the third thing to notice is that the first thing is a result of the second.
- Create a new Java application:
- Name it "QuoteClient":
When you click finish, you have a new Java application, with nodes in the Projects window.
- Right click the project node in the Projects window and choose New > Web Service Client. In the New Web Service Client wizard, paste this in the WSDL URL field:
http://saintbook.org/MightyMaxims/MightyMaxims.asmx?WSDL
Type client in the Package field. Now the wizard should look as follows:
Click Finish.
- You now see a new node, called "Web Service References" in the Projects window. Expand that node and continue expanding until you see the final node, which represents the operation, called "ForToday", as shown below:
- Simply drag that final node, with your mouse, into the Main.java class's main method. That's right, just drag and drop. When you do so, you see a snippet of code added to the method:
- Pat yourself on the back. You've just completed your application.
- Right-click the project node and choose Run. When you used the Web Service Client wizard, the IDE generated client stubs for you. When you added the code snippet to the Main.java class, you generated all the JAX-WS code needed for using the client stubs to contact the web service. And, as specified, in the code snippet, the result is written to the Output window, as shown below:
If you are not interested in creating a plugin for NetBeans IDE or for some other application on the NetBeans Platform, you can stop here. You've now learned everything you need to learn for consuming a web service in a Java application. If, however, you want to integrate the web service client into the IDE, read on...
- Look in the Files window. When you ran the application, the project was built, and the dist folder now contains a JAR file, which is where the application is archived:
We will need that JAR when we create our extension to the IDE.
- Create a module suite (in the New Project wizard, choose NetBeans Modules, then Module Suite). Name it whatever you like and complete the wizard. In the Projects window, right-click the module suite's Modules node. Choose Add New Library and then browse to the JAR file shown above. Finish the wizard, using whatever values you like.
- Now we will create a module project where we will add a menu item that will invoke the web service. So, right-click the module suite's Modules node again and this time choose Add New. Name the project whatever you like and finish the wizard.
- In the module project, set a dependency on the library wrapper module. Also set a dependency on the I/O APIs, so that we can write to the Output window. (Use the Libraries panel of the Project Properties dialog box to set dependencies.)
- On the library wrapper module project, set dependencies on these two modules (make sure to select the "Show Non-API Modules" checkbox first):
Click OK in the dialog above. For the first of the above, you will now need to click 'Edit' and click the 'Implementation Version' radiobutton, because here you are not dealing with an API, i.e., you cannot extend the module, you only need it at runtime to activate the web service.
- Use the New Action wizard to create an action that is always enabled. Position the action anywhere and name it anything. Below, you can see how my menu item ended up being labeled 'Inspire Me!':
- When the New Action wizard is finished, rewrite the performAction method as follows:
public void performAction() { try { //Create a new tab and name it 'Inspiration': InputOutput io = IOProvider.getDefault().getIO("Inspiration", true); // Call Web Service Operation: client.Maxim service = new client.Maxim(); client.MaximSoap port = service.getMaximSoap(); // Receive the thought for the day, as a string: java.lang.String result = port.forToday(); //Print thought for the day to Output window //(the only reason why we use Err instead of Out //is so that we can get a red font for our //thought for the day): io.getErr().println(result); //This opens the Output window if closed //and/or brings it into focus if not already in focus: io.select(); } catch (Exception ex) { // TODO handle custom exceptions here } } - Run the module suite. A new instance of the IDE starts up. The two modules (i.e., the library wrapper module and the module project) are installed. A new menu item is added to whatever menu you specified in the New Action wizard. When you click the menu item, the Output window opens and receives focus, the web service is contacted, and the result is printed to the Output window.
You're finished. And how much coding have you done? Three lines, just for creating a new tab in the Output window, writing our results to it, and for opening the Output window. That's all. And in return you have a "thought for the day" integrated in your application.
Sep 03 2007, 07:47:15 AM PDT Permalink


