Web services are the perfect way to share information and even functionality between applications. It provides means to develop client-server applications where the client and application doesn’t need to be even coded on the same programming language! For example you can have a server interacting with a PC or a mobile phone application via web services. Web services are a great way to providing integration of your application with other applications. It also provides means to extend your application like a client for mobile phones. Depending on your application, there are 3 different types of web services:
- Remote Procedure Calls (RPC)
- Service Oriented Architecture (SOA)
- Representational State Transfer (REST)
For more information go here: http://en.wikipedia.org/wiki/Web_service
NetBeans provides support for all of them. There’s plenty of documentation on how to generate Web Services here: http://netbeans.org/kb/trails/web.html. Consumption of RPC Web services is well supported and documented here: http://netbeans.org/kb/docs/websvc/jersey-rcp-client.html
Consuming JAX-WS Web Services
JAX web services are not yet directly supported for RCP. The approach is creating the client in a plain Java project and either copy and paste the source or create a wrapper library. I suggest the later to make things easier for changes on the client by using the current support on the IDE.
To keep things simple we’ll create a simple web service to be consumed by our client.
Here’s our to-do list:
- Create Web Service for testing
- Create Web Service client
- Wrap Web Service Client in a Library module
- Use client from RCP application
All the hard work is already done by the IDE. It all comes down to consume the generated classes in out module. All project folders should not have spaces in them due to a bug: https://netbeans.org/bugzilla/show_bug.cgi?id=195730 Since the web service type is not supported like RCP Web Services, we need to create a Java Class Library first.
- On the IDE select File>New Project. Then Java>Java Class Library.
- Select the folder and name of the project and click finish.
- Right click the project you just created and select Web Service Client.
- The following step will vary depending on what's the source of the Web Service. In our case select Project and browse for the web project containing the web service to consume. Select a package for the generated classes to go into and click finish. Make sure to build the project so the library is generated.
If you use a web project for source the project needs to be opened in the IDE and the deployed.
- Now let's wrap the library into a Module. Select File>New Project>NetBeans Modules>Library Wrap Module. (this assumes that a Module Suite already exists, if not create one before this step)
- Press Next.
- Select the generated library and select finish (See Linking a wrapper with a project for details on how to automate this process) .
At this point we have a client wrapped and part of our module suite. Now let's write the module that will consume the web service.
- Right click the module folder within your Suite and select Add New.
- Give the module a name making sure there's no spaces on the name/folder.
- Finish the Wizard.
- On the new module, create a new Top Component (New>Window)
- Create the following components:
- On the button's action listener put this code:
int o1, o2; o1 = Integer.valueOf(op1.getText()); o2 = Integer.valueOf(op2.getText()); result.setText(service.getServicePort().add(o1, o2).toString());
This assumes that op1 and op2 are the files names and result is the output text box.
- Create a private global variable for the service like this:
private ServiceService service = new ServiceService();
- Now run the project and enjoy the results
Linking a wrapper with a project
See http://wiki.netbeans.org/DevFaqWrapperModules for methods to link the wrapper module with the source project. This will make propagating the changes to the source automatic.
Creating SOAP Web Services
See http://wiki.netbeans.org/SOAPNetBeans6 for instructions to create a SOAP web service.
- Home to NetBeans Platform Cookbook