Developing your first Web Service with Java EE 5 and NetBeans
In this article we aim to show how easy it is to generate simple webservices using Java EE 5 deploying to the Glassfish application server. The code in this article has been developed with beta 1 of NetBeans 5.5 with the enterprise pack installed and is running on build 48 of the Glassfish application server.
To create a web service, first we need to create a project within NetBeans. Select File | New Project from the NetBeans main menu. On the resulting dialog, select the Enterprise category and choose Enterprise Application. On the resultant dialog, enter the project name as HelloWebService and choose a location for the project. Ensure that the Glassfish server is selected and that the J2EE version is set to Java EE 5. Since we are creating a simple webservice, select to create an EJB Module only. When this information has been specified, press the Finish button to create the project.
The project explorer now shows the HelloWebService and the HelloWebService-ejb nodes. Right click on the HelloWebService-ejb node and select the New | Web Service menu option. In the resultant dialog, enter the Web Service Name as HelloWorldService and enter the package as helloworld. Ensure that an empty web service is created as shown in the following screen shot and select the Finish button to create the web service.
After selecting the Finish button, the source code for the web service will be opened in the file HelloWorldService.java. Add the following code into the class to create the web service method.
@WebMethod Ensure that the project can be build by pressing F11 or selecting the Build Main Project menu option. If everything builds correctly, right click on the HelloWebService project node and select the Deploy Project menu option. This will start the application server (Glassfish in our sample) and deploy the sample web service. After a few seconds, confirmation will be shown in the project output that the webservice has been successfully deployed to the server.
Testing the web service in NetBeans 5.5 is just as easy as creating the web service. Open up the Web Services node under the project as shown in the following screen dump and select the Test Web Service menu option.
This will cause the default browser to be opened with a test page being displayed for testing the web service as shown in the following screen shot.
Enter your name in the edit box towards the bottom of the screen and press the sayHellobutton. This will invoke the web service and display a HTML page showing the results of the web service and details of the SOAP request and response.
This article shows how simple web services can easily be developed and tested in Java EE 5 using the Glassfish application server. Further information about NetBeans can be found on the NetBeans site. Further information on the Glassfish application server can be found on the Glassfish site.
You can discuss this article here.
|
|