JBoss.orgCommunity Documentation

JBoss Web Services User Guide

Version: 3.2.1.GA


1. JBossWS Runtime and Tools support Overview
1.1. Key Features of JBossWS
1.2. Other relevant resources on the topic
2. Creating a Simple Web Service
2.1. Generation
3. Creating a Web Service using JBossWS runtime
3.1. Creating a Dynamic Web project
3.2. Configure JBoss Web Service facet settings
3.3. Creating a Web Service from a WSDL document using JBossWS runtime
3.4. Creating a Web service from a Java bean using JBossWS runtime
4. Creating a Web Service Client from a WSDL Document using JBoss WS
5. JBoss WS and development environment
5.1. JBossWS Preferences
5.2. Default Server and Runtime
6. Sample Web Service wizards
6.1. Sample Web Service
6.1.1. Generation
6.1.2. Deployment
6.2. Sample RESTful Web Service
7. RestEasy simple project example
7.1. The example project
8. Web Service Test View
8.1. Preliminaries
8.2. Testing a Web Service
8.3. Testing a RESTful Web Service
8.3.1. RestfulSample project
8.3.2. RESTEasy sample project

JBossWS is a web service framework developed as a part of the JBoss Application Server. It implements the JAX-WS and JAX-RS specifications. JAX-WS (Java API for XML Web Services) defines a programming model and run-time architecture for implementing web services in Java, targeted at the Java Platform, Enterprise Edition 5 (Java EE 5). JAX-RS (Java API for RESTful Web Services) is a Java API that supports the creation of Representational State Transfer (REST) web services, using annotations.

JBossWS integrates with most current JBoss Application Server releases as well as earlier ones, that did implement the J2EE 1.4 specifications. Even though JAX-RPC, the web service specification for J2EE 1.4, is still supported JBossWS does put a clear focus on JAX-WS.

JBossWS Tool works with JBossWS Runtime. Users can easily create, deploy and run a Web Service(WSDL base or RESTful base) and a Web Service Client using JBossWS Tool and JBossWS Runtime.

Also JBossWS Tool gives a way to test a web service running on a server.

This chapter describes how to create a simple web service.

A simple web service can be created by using the Simple Web Service wizard as described in Generate a simple web service

Procedure 2.1. Generate a simple web service

  1. Access the New - Select a wizard dialog

    1. Right click on the project name in the Project Explorer view.

    2. Select NewOther.

    3. Expand the Web Services folder and click on the Simple Web Service option.

    Result:

    The New - Select a wizard dialog displays with the selected wizard type highlighted.

    Figure 2.1. The New - Other (Wizard selection) dialog


  2. Access the Simple Web Service dialog

    Click the Next button to proceed.

    Result:

    The Simple Web Service - Project and Web Service Details dialog displays.

    Figure 2.2. Simple Web Service - Project and Web Service Details


  3. Define the service attributes

    Define the project, web service, package and class names according to the options displayed in Table 2.1, “Project and Web Service Details”


  4. Generate the web service

    Click the Finish button to complete the web service setup.

    Result:

    The web service classes will be generated and the web.xml file updated with the deployment details if the Update web.xml option was selected.

    Figure 2.3. Created Simple Web Service


In this chapter we provide you with the necessary steps to create a Web Service using JBossWS runtime. First you need to create a Dynamic Web project:

If you have already created a new Dynamic Web project and not set the JBoss Web Service facet to the project, the next step is to add JBoss Web Service facet to the project. Right-click on the project, select its Properties and then find Project Facets in the tree-view on the left-side of the project properties dialog. Tick on the check box for JBoss Web Services. You will see what like this:


At the bottom-left of the right-side of the project properties dialog, there is a error link: Further configuration required... . You must click the link to set more information about JBoss Web Service facet.

Click on the Further configuration required... link. In the opened window


Server Supplied JBossWS Runtime: If you have already set a JBoss runtime to the project's target runtime, you may choose Server Supplied JBossWS Runtime and then click Ok to finish the configuration of JBoss Web Service facet.

If the project has no Target Runtime settings, you should check the second radio button and specify a JBossWS runtime from the list. You also can create a new JBossWS runtime, click on the New... button will bring you to another dialog to configure new JBossWS runtime.


See how to configure a new JBossWS runtime in the Chapter 5, JBoss WS and development environment section.

After setting the information about JBoss Web Service facet, for saving the result, you should click the Apply or OK button at the bottom-right of the right-side of the project properties dialog.

In this chapter we provide you with the necessary steps to create a Web Service from a WSDL document using JBossWS runtime.

Make sure that you have already created a dynamic Web project with JBoss Web Service facet installed and set the necessary preferences through the Preference menu.

See how to make it in the Section 3.1, “Creating a Dynamic Web project”, Section 3.2, “Configure JBoss Web Service facet settings” and Chapter 5, JBoss WS and development environment sections.

To create a Web Service using JBossWS runtime select File > New > Other > Web Services > Web Service to run Web Service creation wizard.

Let's get through the wizard step-by-step:


  • Select the stages of Web service development that you want to complete using the slider:

    • Develop: this will develop the WSDL definition and implementation of the Web service. This includes such tasks as creating modules that will contain generated code, WSDL files, deployment descriptors, and Java files when appropriate.

    • Assemble: this ensures the project that will host the Web service or client gets associated to an EAR when required by the target application server.

    • Deploy: this will create the deployment code for the service.

    • Install: this will install and configure the Web module and EARs on the target server.

    • Start: this will start the server once the service has been installed on it. The server-config.wsdd file will be generated.

    • Test: this will provide various options for testing the service, such as using the Web Service Explorer or sample JSPs.

  • Select your server: the default server is displayed. If you want to deploy your service to a different server click the link to specify a different server.

  • Select your runtime: ensure the JBoss WS runtime is selected.

  • Select the service project: the project selected in your workspace is displayed. To select a different project click on the project link. If you are deploying to JBoss Application Server you will also be asked to select the EAR associated with the project. Ensure that the project selected as the Client Web Project is different from the Service Web Project, or the service will be overwritten by the client's generated artifacts.

  • If you want to create a client, select the type of proxy to be generated and repeat the above steps for the client. The better way is to create a web service client project separately.

First, select Top down Java bean Web Service from the Web Service type list, and select a WSDL document from workspace, click on the Server name link on the page will bring you to another dialog. Here you can specify the server to a JBoss Server and Web Service runtime to JBossWS runtime:


Click on the Finish button to see the next wizard view opened:


Click on the Next button to proceed:


On this page, the default package name comes from the namespace of the WSDL document, you also can change it to any valid package name you want. JAX-WS specification should be set to 2.0 if your JBossWS runtime in JBoss Server is JBossWS native runtime. You can specify a catalog file and binding files if you have them. If you want the wizard to generate empty implementation classes for the Web Service, check the Generate default Web Service implementation classes check box. If you want to update the default Web.xml file with the Web Service servlets configured, check the Update the default Web.xml check box. Click on the Next or on the Finish button to generate code.

Once the Web Service code is generated, you can view the implementation class and add business logic to each method.


View the Web.xml file:


In the next chapter you will find out how to create a Web service from a Java bean.

To create a Web service from a bean using JBoss WS:

Setup Chapter 5, JBoss WS and development environment.

Create Section 3.1, “Creating a Dynamic Web project”.

Note

To use the Simple Web Service wizard to create this Web Service, replace the Class and Application Class fields with your specific classes, within the instructions in Chapter 2, Creating a Simple Web Service.

Section 3.2, “Configure JBoss Web Service facet settings”

Create a Web Service from a java bean:

After the Web Service has been created, the following options may become available depending on the options selected:

In the next chapter you will be able to create a Web Service Client from a WSDL document using JBoss WS.

To create a Web Service Client from a WSDL Document using JBoss WS you need to fulfil the following steps:

Setup Chapter 5, JBoss WS and development environment.

Section 3.1, “Creating a Dynamic Web project”.

Section 3.2, “Configure JBoss Web Service facet settings”.

Then you can create a Web Service Client from a WSDL document:

After the Web Service Client has been created, the following may occur depending on the options you selected:

JBoss WS use a Java class to test Web Service. A client sample class will be generated, you may run this client as a java application to call a web service.

In this chapter you will learn how to change JBossWS preferences and how to set default server and runtime.

JBoss Tools includes wizards for the creation of sample web services. These include:

  • Create a sample Web Service for a JAX-WS web service; and

  • Create a sample RESTful Web Service for a JAX-RS web service.

These wizards are used within a Dynamic Web project. A dynamic web project can be created by following the steps in Creating a dynamic web project.

Procedure 6.1. Creating a dynamic web project

  1. Access the New Project Dialog

    Select FileNewProject

    Result:

    The New Project screen displays.

  2. Define the Project Type

    1. Click the Dynamic Web Project label by expanding the Web folder.

    2. Click the Next button to proceed.

    Result:

    The New Dynamic Web Project screen displays.

    Figure 6.1. Dynamic Web Project Attributes


  3. Define the Project Attributes

    Define the Dynamic Web Project attributes according to the options displayed in Table 6.1, “New Dynamic Web Project”

    Table 6.1. New Dynamic Web Project

    FieldMandatoryInstructionDescription
    Project nameyesEnter the project name.The project name can be any name defined by the user.
    Project locationyesClick the Use default location checkbox to define the project location as the Eclipse workspace or define a custom path in the Location field.The default location corresponds to the Eclipse workspace.
    Target runtimenoSelect a pre-configured runtime from the available options or configure a new runtime environment.

    The target runtime defines the server to which the application will be deployed.

    Dynamic web module versionyesSelect the required web module version.

    This option adds support for the Java Servlet API with module versions corresponding to J2EE levels as listed in Table 6.2, “New Dynamic Project - Dynamic web module version”.

    ConfigurationyesSelect the project configuration from the available options.

    The project can be based on either a custom or a set of pre-defined configurations as described in Table 6.3, “New Dynamic Project - Configuration”.

    EAR membershipnoAdd the project to an existing EAR project.

    The project can be added to an existing EAR project by selecting the checkbox. Once checked, a new EAR project can be defined by clicking the New Project button.

    Working setsnoAdd the project to an existing working set.

    A working set provides the ability to group projects or project attributes in a customized way to improve access. A new working set can be defined once the Select button has been clicked.




  4. Access the Java sub-dialog

    Click Next to proceed.

    Result:

    The New Dynamic Web Project - Java dialog displays.

  5. Define the source and output folders

    Define the Dynamic Web Project source and output folders by adding or editing folders as required.

  6. Access the Web Module sub-dialog

    Click Next to proceed.

    Result:

    The New Dynamic Web Project - Web Module dialog displays.

    Figure 6.2. New Dynamic Web Project - Web Module


  7. Enter the web module settings

    Define the settings as listed in Table 6.4, “New Dynamic Web Project - Web Module” including the root folder for path names in the web project context and the name of the web content directory.


  8. Open the Java EE perspective.

These sections describe how to generate and deploy a sample web service.

A sample web service can be created by using the Create a Sample Web Service wizard as described in Generate a sample web service

Procedure 6.2. Generate a sample web service

  1. Access the New - Select a wizard dialog

    1. Right click on the project name in the Project Explorer view.

    2. Select NewOther.

    3. Click the Create a Sample Web Service label by expanding the Web Services folder.

    Result:

    The New - Select a wizard dialog displays with the selected wizard type highlighted.

  2. Access the Generate a Sample Web Service dialog

    Click the Next button to proceed.

    Result:

    The Generate a Sample Web Service - Project and Web Service Name dialog displays.

    Figure 6.3. Generate a Sample Web Service - Project and Web Service Name


  3. Define the service attributes

    Define the project, web service, package and class names according to the options displayed in Table 6.5, “Project and Web Service Name”


  4. Generate the web service

    Click the Finish button to complete the web service setup.

    Result:

    The web service classes will be generated and the web.xml file updated with the deployment details.

  5. Browse the HelloWorld.java class

    Double click the HelloWorld.java class and note the annotated class name and method. These annotations identify the web service entities to the server.

    Figure 6.4. web.xml


  6. Browse the web.xml deployment decriptor

    Double click the web.xml file and note the servlet mapping as defined in Figure 6.3, “Generate a Sample Web Service - Project and Web Service Name”. Note also that:

    • the main servlet for the application is org.jboss.samples.webservices.HelloWorld which is given the custom name HelloWorld; and

    • the main servlet is mapped to the particular url /HelloWorld [1].

    Figure 6.5. web.xml


    Upon start up, the server will write a WSDL file to the server-profile/data/wsdl/ directory and the WSDL can be accessed with http://localhost:8080/ProjectName/[1]?WSDL or, http://localhost:8080/WebServiceSample/HelloWorld?WSDL.

Once created, the sample web service can be deployed to the target runtime as described in Export the project as a Web Archive (WAR).

A sample Restful web service can be generated by following the steps outlined in Generate a sample RESTful web service.

Procedure 6.4. Generate a sample RESTful web service

Target runtime must have RESTEasy installed

The sample RESTful web service will not work unless it is deployed to a server with RESTEasy installed.

  1. Access the New - Select a wizard dialog

    1. Right click on the project name in the Project Explorer view.

    2. Select NewOther.

    3. Click the Create a Sample RESTful Web Service label by expanding the Web Services folder.

    Result:

    The New - Select a wizard dialog displays with the selected wizard type highlighted.

  2. Access the Generate a Sample RESTful Web Service dialog

    Click the Next button to proceed.

    Result:

    The Generate a Sample RESTful Web Service - Project and Web Service Name dialog displays.

    Figure 6.7. Generate a Sample RESTful Web Service - Project and Web Service Name


  3. Define the service attributes

    Define the project, web service, package and class names according to the options displayed in Table 6.7, “Project and Web Service Name”


  4. Generate the web service

    Click the Finish button to complete the web service setup.

    Result:

    The web service classes will be generated and the web.xml file updated with the deployment details.

  5. Browse the MyRESTApplication.java class

    Double click the MyRESTApplication.java class and note the constructor instantiating objects of type HelloWorldResource. The relevance of this will be discussed shortly.

    Figure 6.8. Application Class - MyRESTApplication.java


  6. Browse the HelloWorldResource.java class

    Double click the HelloWorldResource.java class and note the JAX-RS annotated path and the annotated GET method.

    Figure 6.9. HelloWorldResource.java


  7. Browse the web.xml deployment descriptor

    Double click the web.xml file and note the jax.ws.rs.Application parameter mapped to the Application class. Note also that:

    • the main servlet for the application is org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher which is given the custom name Resteasy; and

    • the main servlet is not mapped to a particular url as indicated by /*.

    The url for sending GET requests can be resolved as follows:

    1. Identify the Application Class as defined in the deployment descriptor.

    2. Note the object type instantiated in the Application class and added to the singleton set: HelloWorldResource.

    3. Note the JAX-RS annotated path declared in the corresponding HelloWorldResource class: @Path("/MyRESTApplication") [1].

    Figure 6.10. web.xml


    The url for sending GET requests is therefore http://localhost:8080/ProjectName/[1] or, http://localhost:8080/RestfulSample/MyRESTApplication.

JBoss Tools includes many example projects which are available by selecting HelpProject Examples. The following sections describe setting up the example RESTEasy project. This project serves as a good example for testing the numerous Web Service Test View functions.

Once the required plugins have been installed, the example project can be set up as described in JBoss Tools New Example Project

Procedure 7.1. JBoss Tools New Example Project

  1. Access the New Example Project Dialog

    Select HelpProject Examples

    Result:

    The New Example Project dialog displays.

  2. Define the Example Project Type

    1. Click the RESTEasy Simple Example label by expanding the RESTEasy node.

    2. Click the Finish button to complete the project set up.

    Result:

    The simple project is configured and ready to build.

    Project requirements

    In the event that a message is displayed indicating some requirements could not be configured, click the Details button followed by the Fix button to rectify the problem. The message will be displayed as a result of missing plugins or a requirement to select or configure a suitable runtime.

  3. Build the project

    Right click on the project name and select Run AsMaven package

    Result:

    The simple.war file is written to the project's 'target directory.

  4. Deploy the project

    Copy the simple.war file to the deploy directory of the required server profile such as the all profile.

    Result:

    The simple.war file is written to the target directory.

  5. Determine the URL for the web service

    Double click the web.xml file and note the jax.ws.rs.Application parameter mapped to the Application class. Note also that:

    • the main servlet for the application is org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher which is given the custom name Resteasy; and

    • the main servlet is mapped to the url /rest-services/* [1].

    The url for sending GET requests can be resolved as follows:

    1. Identify the Application class as defined in the deployment descriptor.

    2. Note the object type (CustomerResource) instantiated in the Application class (ShoppingApplication) and added to the singleton set (singletons.add(new CustomerResource())).

    3. Note the JAX-RS annotated path declared in the corresponding CustomerResource class: @Path("/customers") [2].

    Figure 7.1. web.xml


    The url for sending GET requests can be formed from http://localhost:8080/ProjectName/[1]/[2] or, http://localhost:8080/simple/rest-services/customers..

JBoss Tools provides a view to test web services. The Web Services Test View can be displayed by following the steps in Web Services Test View.

Procedure 8.1. Web Services Test View

The following sections describe testing JAX-WS and JAX-RS web services including the necessary preliminary steps.

The following procedure describes the steps to perform before testing a web service.

Procedure 8.2. Testing a JAX-RS web service

A JAX-WS web service can be tested by using the Web Service Tester View displayed in Figure 8.1, “Web Service Test View”. The JAX-WS test is specified by:

  1. Selecting the JAX-WS combobox option.

  2. Entering the location of the WDSL file.

Step 2 can be performed in a number of ways including:

  • entering the location directly in the editable dropdown list; or

  • clicking the Get from WSDL file button and entering the URL, Eclipse workspace or File system details.

Testing a JAX-WS web service demonstrates testing the WebServiceSample project developed in Generate a sample web service.

Procedure 8.3. Testing a JAX-WS web service

  1. Following the preliminary steps described in Testing a JAX-RS web service, select JAX-WS from the available combo box options.

    Result:

    The SOAP message details are displayed in the Request Body textbox of the Request Details panel.

    Figure 8.3. JAX-WS Body Text


  2. Enter the location of the WSDL file in the editable dropdown list. The location for the WebServiceSample web service is http://localhost:8080/WebServiceSample/HelloWorld?WSDL

  3. Click the Invoke button.

    Result:

    The Select WDSL dialog appears.

    Figure 8.4. Select WSDL


  4. Select the required service attributes

    Select the Service, Port and Operation from the comboboxes and click OK.

    Results:

    The <soap:Body/> section of the SOAP message is filled with the SayHello message details.

    Figure 8.5. JBoss Tools Project Creation


    The response header details are returned.

    Figure 8.6. JBoss Tools Project Creation


    The response message body is displayed in the Response Body textbox.

    Figure 8.7. JBoss Tools Project Creation


    These results indicate a successful test.

Testing a RESTful ( JAX-RS ) web service is achieved by following a similar procedure to testing a JAX-WS web service. Instead of selecting the JAX-WS option in the combobox, the JAX-RS service is invoked by sending HTTP method requests of the form OPTIONS, GET, POST, PUT and DELETE. As there is no WSDL file associated with a JAX-RS service, the available options can be determined by selecting OPTIONS in the combobox.

A JAX-RS web service can be tested by using the Web Service Tester View displayed in Figure 8.1, “Web Service Test View”. The JAX-RS test is specified by:

  1. Selecting the OPTIONS combobox option.

  2. Entering the url of the JAX-RS web service.

The test procedure is discussed in the following sections for both the RestfulSample and the RESTEasy sample projects developed earlier.

Procedure 8.4. RestfulSample test

    1. Query the available options

      Select OPTIONS from the available combobox options.

    2. Enter the url of the web service in the editable drop-down list: http://localhost:8080/RestfulSample/MyRESTApplication.

    3. Click the Invoke button

      Result:

      The Response Headers text area indicates that the allowed options are [GET, OPTIONS, HEAD] as shown in Figure 8.8, “JAX-RS Response Header Text”.

      Figure 8.8. JAX-RS Response Header Text


  1. Test the GET request

    1. Having established that the GET request is valid, select GET from the available combobox options.

    2. Click the Invoke button.

      Result:

      The Response Body text area displays the expected “Hello World” text as shown in Figure 8.9, “JAX-RS Response Body Text”.

      Figure 8.9. JAX-RS Response Body Text


Procedure 8.5. Testing a JAX-RS web service- POST and GET requests

    1. Query the available options

      Following the preliminary steps described in Testing a JAX-RS web service, select the OPTIONS method from the operations text area.

    2. Enter the url of the web service in the editable drop-down list http://localhost:8080/simple/rest-services/customers.

    3. Click the Invoke button

      Result:

      The Response Headers text area indicates that the allowed options are [POST, OPTIONS] as shown in Figure 8.10, “JAX-RS RESTEasy project Body Text”.

      Figure 8.10. JAX-RS RESTEasy project Body Text


  1. Test the POST option

    1. Select POST method in the the operations drop-down list.

    2. We will post xml data to this particular web service. Complete the header details by entering content-type=application/xml in the text area and click Add to add it to the Headers list.

      Result:

      The content-type is added to the Headers list as shown in Figure 8.11, “content-type header”.

      Figure 8.11. content-type header


    3. Enter customer details

      Enter the customer details in the Body Text area as displayed in Figure 8.12, “Customer data”.

      Figure 8.12. Customer data


    4. Click the Invoke button.

      Result:

      The Response Headers area indicated that a record was created and lists the location as http://localhost:8080/simple/rest-services/customers/1 as shown in Figure 8.13, “Customer added”.

      Figure 8.13. Customer added


      The console also indicates the successful creation of the customer: 10:44:33,846 INFO [STDOUT] Created customer 1

  2. Test the GET option

    1. Select the GET method in the the operations drop-down list.

    2. We will retrieve the record created in the previous step. Enter the url for the record returned in the previous step http://localhost:8080/simple/rest-services/customers/1

    3. Click the Invoke button.

      Result:

      The Response Headers area indicates a [HTTP/1.1 200 OK] response and the customer data is retrieved and displayed in the Response Body area as shown in Figure 8.14, “GET response”.

      Figure 8.14. GET response


  3. Test the PUT option

    1. Editing a record is achieved by using the PUT method. Select the PUT method in the operations drop-down list.

    2. Enter the url of the record to be edited http://localhost:8080/simple/rest-services/customers/1

    3. Enter the data in the Body Text area. Replace the first-name with a different entry as in Figure 8.15, “Updated customer data”

      Figure 8.15. Updated customer data


    4. Ensure that the content-type=application/xml header is in the Headers list.

    5. Click the Invoke button.

      Result:

      The Response Headers area indicates a No Response ([HTTP/1.1 204 No Content]) Figure 8.16, “Response header following PUT”.

      Figure 8.16. Response header following PUT


      In this instance, the console does not indicate an update was performed, however, the console may provide useful information following an operation.

  4. Check the updated data with a GET

    Perform a GET operation by following the steps in Step 3.

    Result:

    The Response Body area displays the updated data.

    Figure 8.17. Customer data updated


  5. Test the DELETE option

    1. Deleting a record is a similar process to posting. Select the DELETE method in the operations drop-down list.

    2. Enter the url of the record to be deleted http://localhost:8080/simple/rest-services/customers/1

    3. Click the Invoke button.

      Result:

      The Response Headers area indicates a No Response ([HTTP/1.1 204 No Content]) as was the case for the PUT operation in Figure 8.16, “Response header following PUT”.

      Once again, the console does not indicate an update was performed, however, the console may provide useful information following an operation.

  6. Check the DELETE operation with a GET

    Perform a GET operation by following the steps in Step 3.

    Result:

    The Response Body area returns an error report indicating that The requested resource () is not available and the Response Headers area returns a [HTTP/1.1 404 Not Found].

    Figure 8.18. Customer data deleted


    The response header and body messages indicate that the data was successfully deleted.