EclipseToNetBeansSOAP

Web Services with NetBeans (for Eclipse users)

(initial version 12-2007 by Kristian Rink, kawazu <at> zimmer428 <dot> net, see PageHistory for modifications)

Contents


(0) Rationale and prerequisites

Developing SOAP-based web services is of interest in these days of SOA. Current development platforms--both server- and IDE-side--provide extensive support for creating web services. From a SOAP/WS developers point of view, moving from Eclipse to NetBeans makes a number of things easier, once you get used to the differences between the two tools. This paper demonstrates these differences by creating a simple application environment in both tools:

  1. A simple Java class that doesn't do much (
    Pointless.java
    ), exposed as a web service.
  2. A simple Java standalone application client that calls the Pointless service.

As there are indeed some differences between how this is handled in NetBeans and in Eclipse, I preferred not to simply migrate an existing project from an Eclipse workspace to NetBeans. Instead, I outline the procedure required to get this functionality implemented step-by-step using both IDEs, pointing out differences as they arise. Tools used:

  • NetBeans 6.0p1
  • Eclipse Europa 3.3.1
  • Apache Tomcat 6.0.14 (configured in both NetBeans and Eclipse)

(1) The Eclipse Way Of Things

Start Eclipse with a clean workspace containing a working Tomcat 6 runtime and server configuration. Use the "Dynamic Web Project" provided by Eclipse WTP as a starting point:

File:EclipseToNetBeansSOAP/01-eclipse-new EclipseToNetBeansSOAP.jpg

Name the project in this example "ws". Keep the defaults for everything else and click "Finish": File:EclipseToNetBeansSOAP/02-eclipse-new EclipseToNetBeansSOAP.jpg

The workspace displays the newly created project in the "Project Explorer" view.

Now we add our business logic. Right-click the project name and choose "New" -> "Other" and select "Java Class". Name the new class "Pointless" (rather well describing what it does...). It should be located in
services.impl
:

File:EclipseToNetBeansSOAP/03-eclipse-svcclass EclipseToNetBeansSOAP.jpg

Click "Finish", and wait for Eclipse to bring up the editor view of the newly created class. Once the view appears, add a simple method that generates some return data we can
System.out.println()
in our client later on. In this example, the method should be called {getRemoteDate()}. If necessary, make the IDE fix up your imports (using CTRL+Shift+O).

File:EclipseToNetBeansSOAP/03-eclipse-svcclass-code EclipseToNetBeansSOAP.jpg

Our logic is now implemented. Next we wrap the implementation in a web service and deploy it to the servlet container. To do so, right-click the project name in the "Project Explorer", select "New" -> "Other" (or select "New...", "Other from the "File" menu) and choose "Web Service":

File:EclipseToNetBeansSOAP/04-eclipse-ws-new EclipseToNetBeansSOAP.jpg

The Eclipse Web Service creation wizard opens. You can set several options in this wizard. Set the options as shown:

  • Web Service Type: bottom-up web service
  • Service implementation: use "Browse..." and search for your
    Pointless
    class
  • Set the "build level" (the blue bar on the left of the image) to "deploy web service" by pushing the slider to the right position.

Note the "Web service runtime: Axis" output on the right. Eclipse relies solely upon Apache Axis or Axis2 to create web services implementation.

File:EclipseToNetBeansSOAP/05-eclipse-ws-conf EclipseToNetBeansSOAP.jpg

After you set the options, end the wizard by clicking "Finish". Wait for the IDE to prepare your web service environment. When the IDE has finished, your project inside the "Project Navigator" should appear as depicted below. Look at
web.xml
for more clear evidence that Apache Axis is present:

File:EclipseToNetBeansSOAP/06-eclipse-ws-outcome EclipseToNetBeansSOAP.jpg

Because you chose "Deploy web service" earlier while creating the web service, by now your local Tomcat container should be running and the service should be available. If not, right-click the project in "Project Explorer", choose "Run as..." and "Run on server". Once running, you can access the service and its WSDL description using http://localhost:8080/ws/services/Pointless or http://localhost:8080/ws/services/Pointless?wsdl respectively:

File:EclipseToNetBeansSOAP/07-eclipse-ws-runtest EclipseToNetBeansSOAP.jpg

Select and copy the WSDL URL because you need it to build the client. To start building the client, select "File" -> "New..." -> "Other" and create a new "Java Project". Name the project "wsclient" and accept all other default values by clicking "Finish":

File:EclipseToNetBeansSOAP/07-eclipse-wsclient-new EclipseToNetBeansSOAP.jpg

Right-click the "wsclient" project node and select "New" -> "Other" -> "Web Service Client". A web service wizard opens.

File:EclipseToNetBeansSOAP/09-eclipse-wsclient-new EclipseToNetBeansSOAP.jpg

Paste the WSDL URL copied earlier to the "Service definition" field and wait for the tool to fetch and accept the description. Set the "build level" at "Assemble client"--setting it higher doesn't seem to have any effect when building a client in a Java SE standalone application.

File:EclipseToNetBeansSOAP/10-eclipse-wsclient-new EclipseToNetBeansSOAP.jpg

Right here, Eclipse client creation ends. In your Project Explorer, you are now provided with a couple of Java classes representing the web service client itself. Browse through the classes and you discover that
PointlessProxy
is the only class that accesses the remote service. This is not a difficulty in this case, but when the number of services to be proxied increases, {PointlessProxy} can become a bottleneck:

File:EclipseToNetBeansSOAP/11-eclipse-client-stubs EclipseToNetBeansSOAP.jpg

At this point we want to see some output, so create an empty class containing a "main" method, usually called
Runner
in my projects:

File:EclipseToNetBeansSOAP/12-eclipse-wsclient-runner EclipseToNetBeansSOAP.jpg

After you enter the code required for invoking the remote SOAP call and put the result returned to
System.out
, the class should look like this:

File:EclipseToNetBeansSOAP/13-eclipse-wsclient-runner-test EclipseToNetBeansSOAP.jpg

So much for the first part...


(2) NetBeans at your service.

Now let's try the same in Netbeans. Like in Eclipse, start a "Web Application" project, keep the default values and click "Finish".

File:EclipseToNetBeansSOAP/14-nb-webproj EclipseToNetBeansSOAP.jpg

The IDE creates a Web Application named "ws" and opens it in your Projects view.

File:EclipseToNetBeansSOAP/15-nb-webproj-2 EclipseToNetBeansSOAP.jpg

Feel free to delete the "index.jsp" that comes with the skeleton project if you prefer.

File:EclipseToNetBeansSOAP/16-nb-webproj-empty EclipseToNetBeansSOAP.jpg

Now let's create our web service implementation, beginning with our
Pointless
class. Right-click the "ws" project node and select "New" -> "Other". Then select "Java Class" from the "Java" category.

File:EclipseToNetBeansSOAP/17-nb-svcclass EclipseToNetBeansSOAP.jpg

Name the class "Pointless" and make the IDE place it in
services.impl
. Click "Finish" and the "Pointless" class opens in the code editor.

File:EclipseToNetBeansSOAP/18-nb-svcclass-new EclipseToNetBeansSOAP.jpg

Enter the same business method we used in our Eclipse example:

File:EclipseToNetBeansSOAP/19-nb-svcclass-code EclipseToNetBeansSOAP.jpg

We have implemented the service class. Good. At this point, I started searching how to use NetBeans IDE to expose a single class as a Web Service, but I failed to find a wizard to do that--the "Web Service" wizard builds web services only either "from scratch" or using an existing Enterprise Java Beans class. The solution, however, couldn't be easier: In the editor window for the implementation class in which you just added your code, simply add the
@WebService
annotation right before the class declaration. Then fix your imports to make the class {import javax.jws.WebService}, and save the class.

File:EclipseToNetBeansSOAP/21-nb-service-code EclipseToNetBeansSOAP.jpg

NetBeans is busy for a few seconds, and then you see that, in the Projects window, your project structure now contains a "Web Services" entry:

File:EclipseToNetBeansSOAP/20-nb-service EclipseToNetBeansSOAP.jpg

Note also that if you open
Pointless.java
, your code editor allows you to switch between code and "design" views. In the design view, you can add new operations and parameters visually, set reliability / security options for your web service and so on.

File:EclipseToNetBeansSOAP/22-nb-service-design EclipseToNetBeansSOAP.jpg

We won't do anything like that now, however, so we right-click the project and choose either "Run" or "Undeploy and deploy" to get the service to your tomcat container. Depending on what you chose, your browser window automatically opens the appropriate location. If it does not, use http://localhost:8080/ws/PointlessService to see the web services test page. This page contains a link to the WSDL description, which you can copy for use when building the client: File:EclipseToNetBeansSOAP/23-nb-service-test EclipseToNetBeansSOAP.jpg

The next step is the straightforward creation of an empty Java application to house your web service client.

File:EclipseToNetBeansSOAP/24-nb-svcclient EclipseToNetBeansSOAP.jpg

Choose a good name and make the IDE create a Runner class automatically:

File:EclipseToNetBeansSOAP/25-nb-svcclient-create EclipseToNetBeansSOAP.jpg

Now that the empty application is ready, we create the "Web Service Client" itself. Right-click the application node and choose "New" -> "Other" -> "Web Services" -> "Web Service Client". "Project" already should be set to a reasonable value (most likely to be your current test project), so you only have to paste the "WSDL URL" you copied into the service description, choose a meaningful package name and click "Finish". The IDE now provides you with the client to your very basic SOAP service.

File:EclipseToNetBeansSOAP/26-nb-addclient EclipseToNetBeansSOAP.jpg

Yes, you will really "see" it, as the client will appear in your project structure in "Web Service References" as a browseable tree element providing you with direct access to the functions it provides / exposes:

File:EclipseToNetBeansSOAP/27-nb-clientbrowse EclipseToNetBeansSOAP.jpg

This feature proves to be quite useful. We can switch to the code editor displaying the automatically generated "Runner" class and add a web service call simply by dragging in the function reference from "Web Service References" to the body of
main(...)
in the Runner class.

File:EclipseToNetBeansSOAP/28-nb-addclientcall EclipseToNetBeansSOAP.jpg

Nice feature. Finally, let's run the project to see that the service and client work the way they should (if they don't, please see the "Pitfalls" notes below): File:EclipseToNetBeansSOAP/31-nb-run EclipseToNetBeansSOAP.jpg


(3) Conclusions

Yes, in both platforms one possibly could get web services up and running solely working with XML configuration files, classpath entries and code. However, as providing support for jobs like this is one of the things you'd expect to see in a state-of-the-art IDE, this hasn't been an option here. Though both IDEs do well here, the NetBeans tooling is one step ahead in many respects, especially when it comes to client functionality (which is next to non-existent in Eclipse/WTP). Dealing with very simple web services, this is possibly not too much of an issue. When you have a project containing a set of WS clients, however, NetBeans IDE's greater functionality can be of more importance. This is without even getting into exposing EJB components in a JEE container using web services...

The next conclusion is that, while starting to write this page, I was about to dump the service to our local Glassfish testbed environment... which worked well from within NetBeans but drastically failed from within Eclipse, for reasons not yet obvious to me. I will have a look at this later.


(4) Pitfalls and other things worth mentioning

In NetBeans, when you run the service client you might encounter the following exception (that's why I suggested including the
ex.printStackTrace()
earlier):

File:EclipseToNetBeansSOAP/29-nb-failedrun EclipseToNetBeansSOAP.jpg

The problem seems to be that the required JAX-WS libraries aren't automatically added to the client project classpath when you create a web service client. Doing so manually eliminates this problem:

File:EclipseToNetBeansSOAP/30-nb-addlibs EclipseToNetBeansSOAP.jpg

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo