X

Geertjan's Blog

  • September 3, 2007

Java Clients and Web Services in NetBeans IDE 6.0

Geertjan Wielenga
Product Manager
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.

  1. Create a new Java application:

  2. Name it "QuoteClient":

    When you click finish, you have a new Java application, with nodes in the Projects window.

  3. 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.

  4. 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:

  5. 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:

  6. Pat yourself on the back. You've just completed your application.

  7. 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...

  8. 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.

  9. 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.

  10. 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.

  11. 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.)

  12. 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.

  13. 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!':

  14. 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
    }
    }

  15. 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.

Join the discussion

Comments ( 16 )
  • Carl Monday, September 3, 2007

    Geertjan,

    That's the fastest WS Client Code I have ever slung!

    I like that drag and drop.

    I think there is an error on step 5, I think you meant to drag the node into the main method area not the constructor.

    Keep up the awesome work.

    Thanks!


  • Geertjan Monday, September 3, 2007

    Thanks! Fixed it. Glad you liked it!


  • Ernie Rael Thursday, September 6, 2007

    Should step 3 package be client or quoteclient?

    Also, I started with build 200709051200, ide/java only (no web stuff). Didn't have web services menu. Used plugin manager to get "Web Services" plugin. But then after step 5 there are editor highlight complaints about "package client does not exits" (different complaints if I used package quoteclient). In any case compilation failed.

    Also, after step 3 there is an exception.

    I'm guessing I need to install some other (one or more) plugins. I'm posting a generic question about this to nbusers.

    -ernie


  • Geertjan Thursday, September 6, 2007

    Better to install the full IDE in this case. Or, at least, with the web stuff.


  • Geertjan Thursday, September 6, 2007

    And probably you're using JDK 5, so you need to add the JAX-WS 2.1 library to the project. Right-click the Libraries node and then get JAX-WS 2.1 from the Library Manager. Then compilation should work.


  • Ernie Rael Friday, September 7, 2007

    I am using JDK 6, today with build 200709070000 and the Web&JavaEE package. I was confused before because after the drag&drop in step 5 the editor shows syntax errors: package/class client not found. However it does compile and run successfully.

    It looks like the DnD does some real magic to get quoteclient.Main to compile/run, or maybe its just general web related magic, (how/where to find client is not mentioned in Main.java), but the magic isn't enough to get syntax highlighting to work. There's also a warning when it compiles.

    The next time I download daily, I'll just do java base and see if I can now get it to work by only getting the WebServices plugin.

    BTW, thanks a lot for these simple examples. Its a great way for a web novice like myself to get my feet wet.


  • Geertjan Saturday, September 8, 2007

    The client is in the build folder, visible in the Files window. It is regenerated at build time, hence it is not shown in the Projects window (artifacts that are recreated after creation are never shown in the Projects window). Thanks for the experimenting you're doing and also for liking the simple example approach (and for saying so).


  • Ernie Rael Saturday, September 8, 2007

    Starting with NB only having java/ide, all you need to download is the WebServices plugin to complete this example. Hooray.

    I understand that generated code doesn't appear in the project. After the "New Web Service Client" dialog stuff is generated/built; it seem that

    - a "pseudo library" (or something) could hook up things so that syntax hightlighting doesn't give errors. (I still find it weird that there is no mention of "client" in an import statement)

    - the "Web Services Reference" node could have "generate" in its context menu (useful after a clean)

    I guess if one typically packages the generated code in a seperate library/project, then the places it is referenced from will not display errors and so things aren't that bad.


  • Jim Nicolson Tuesday, November 13, 2007

    Just a brief followup on the post by Ernie Rael on September 07, 2007 at 07:59 PM PDT.

    I'm encountering the same problem - I want to be able to create Web Service unit tests using JUnit - oddly enough if a second web reference is added to the same project, the same error highlighting shows up on the second service. However, after this the compile fails on \*both\* services.

    So this is probably a bug - I used the default package so that may be relevant.

    Should say however, the the approach to WS Client is the best I've seen in any IDE so far.


  • Jim Nicolson Tuesday, November 13, 2007

    Forgot to add environment info:

    Product Version: NetBeans IDE 6.0 Beta 2 (Build 200710212201)

    Java: 1.6.0_03; Java HotSpot(TM) Client VM 1.6.0_03-b05

    System: Windows XP version 5.1 running on x86; Cp1252; en_AU (nb)


  • Erik Wednesday, February 6, 2008

    I cant get your example to work. I'm tryed to follow your example from 1 to 7. Does it need some aditional options in the compiler

    Nice job


  • Geertjan Wednesday, February 6, 2008

    Erik, it's impossible to help you. You have given no clue about what's going wrong...


  • Erik Wednesday, February 6, 2008

    Hi Geertjan

    It seems to be a human error... sorry


  • rouskejfan Wednesday, April 30, 2008

    I have a question.

    I created a web service client in NB 6.0.1 an than dragged some methods into my code. Unfortunately i get javax.xml.ws.WebServiceException: Provider com.sun.xml.ws.spi.ProviderImpl not found

    I googled it out and I found that I have to add JAX-WS 2.1 library and it works fine with this library. But the question is why do I have to bundle this library despite the fact that JAX-WS 2.1 is a part of JDK1.6.0_04 and later releases (version of java I am using is 1.6.0_05).

    thx for reply.


  • Mithun Thursday, June 19, 2008

    Hi ,

    i followed the steps but I am getting the result as null after step 7 . what should i do ?

    Please help

    Mithun


  • Mohsan Raza Tuesday, March 19, 2013

    Hi Geertjan,

    I tried to add web service client after wrapping in module project of NetBeans 7.3 but It gives following exception:

    http://jaxb.java.net/nonav/2.2.3/docs/api/javax/xml/bind/JAXBException.html


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

Integrated Cloud Applications & Platform Services