Scout/Tutorial/3.8/webservices/Webservices with JAX-WS
|Download • Git|
|Forums • Blog • Twitter|
- 1 Abstract
- 2 Resources
- 3 Preliminary work
- 4 Ensure JAX-WS support is enabled for your project
- 5 Create Webservice Consumer
- 6 Integrate Webservice Consumer in application
- 7 Log Webservice Consumer's SOAP messages
- 8 Create Webservice Provider
- 9 Log Webservice Providers's SOAP messages
- 10 Test the Webservice Provider
- 11 See Also
This tutorial is a guide to developing JAX-WS webservices in Eclipse Scout. JAX-WS stands for Java API for XML Web Services. The JAX-WS version supported by Eclipse Scout is JAX-WS RI 2.1.6 bundled with Java SE 6. We are not using the lastest version (such as JAX-WS RI 2.2.5) because we encountered a lot of problems in having a newer version aside the Java built-in internal JAX-WS RI 2.1.6 implementation.
The scope of this tutorial is to create a simple Scout application with a webservice consumer and a webservice provider installed. The application will load company data from a Derby database and allow the user to request a company's stock price with a 20 minute delay from the public StockQuoteService. Also, a webservice will be published to access the application's company data from within another application.
The Eclipse projects for this tutorial can be downloaded from our SVN at scout.example. All projects that belong to this tutorial start with the name org.eclipse.scout.tutorial.jaxws.*
In order to run the tutorial you need to have the Derby database installed on your system. A ready-to-go database can be downloaded from org.eclipse.scout.tutorial.jaxws.database.zip. Alternatively, the database can be created from scratch by using the SQL script create_database.sql located in the Plug-In org.eclipse.scout.tutorial.jaxws.database. Learn more on how to setup the derby database from scratch.
First of all, you have to create a new Scout project.
Afterwards, create a Derby SQL Service to access company data and to persist webservice log entries.
In order to display data from the database, create a Company Table Page.
To display stock quote information for a company, create a Company Form.
Ensure JAX-WS support is enabled for your project
In order to have JAX-WS support available in the project, select the project node 'org.eclipse.scout.tutorial.jaxws' in the Scout Explorer. In the Scout Object Properties scroll to the 'Technologies' section and ensure the checkbox 'Webservices with JAX-WS RI 2.1.6' is enabled. If not yet checked, enable the technology and confirm the popup dialog with 'Ok'.
If JAX-WS support is enabled correctly you should see the 'Webservices (JAX-WS RI 2.1.6)' under the server node in the Scout Explorer. 
Create Webservice Consumer
On server node, go to the node
'Webservices (JAX-WS RI 2.1.6)' | 'Consumer' | 'Services'. Right-click on that node to create a new webservice consumer . In the first wizard step, choose the 2nd option
WSDL FROM URL and enter the URL to the WSDL file of the stock quote service provider . In the course of this tutorial this would be http://services.nexus6studio.com/StockQuoteService.asmx?wsdl. By pressing TAB or clicking somewhere outside the URL field, the WSDL file is evaluated. If this is about a valid WSDL file, click Finish to create the webservice consumer. Click here to learn more about webservice consumer creation. In case you encounter problems to build the webservice stub, refer to the Console Output for more information.
Please find the created webservice consumer
StockQuoteServiceSoapWebServiceClient in Scout Explorer . In the Scout Property View of this consumer, you can access the various files such as the WSDL file or the service type. Also, there you find links to rebuild the webservice stub, to change build properties for stub generation process, to edit binding customization files and more. Also, the authentication mechanism can be changed in the section
Authentication. For more information on that Property View, please click here.
Next, we have to configure the endpoint URL of the webservice which is http://services.nexus6studio.com/StockQuoteService.asmx. For the sake of convenience, we hard-code this URL by specifying the property
URL in the Property View . In real life, this would be done in config.ini to distinguish the different systems such as development, integration and production.
Integrate Webservice Consumer in application
Finally, the webservice is ready to be used by our tutorial application. Open the process service
CompanyProcessService and follow the two steps described below:
Add data conversion methods Because data provided by the webservice is only of the type
xsd:string, we have to convert them to their corresponding types. Please add the following conversion methods to your service. Click here to get the source
Change load-method to access webservice We have to change the implementation of
CompanyProcessService#load(CompanyFormData)to access the stock quote webservice and include quote information for that company in the FormData. Please change the service's implementation as following: Click here to get the source
Finally, if you launch the application, you should see something like this:
Log Webservice Consumer's SOAP messages
In order to log the SOAP messages for request and response, we have to add a
LogHandler to the webservice client. In Scout, there already exists a simple version of such a log handler, but the log is only written to the logging facade, not to the database. That is why we write our own
DatabaseLogHandler that inherits from the Scout
Because handlers can be used in both, consumers and providers, they are located in 'Webservices (JAX-WS RI 2.1.6)' | 'Handlers'. Right-click on that node to create a new Handler . In the dialog, enter the following values :
Properties Name: DatabaseLogHandler Package: <do not change> Super Type: LogHandler (click on 'Browse' to search for that type) Run in Scout transaction: check because we are writing to database Session factory: <do not change>
Double-click on the handler and implement it as following (source code):
After the handler is created, go back to the webservice consumer
StockQuoteServiceSoapWebServiceClient. In Scout Property View, click on
Exec Install Handlers to register the handler . Simply add the
DatabaseLogHandler to the list of handlers. Click here to get the source code.
Finally, if you launch the application and open a company, the SOAP messages are written into the database. In turn, if you open a log message, you should see something like this:
Create Webservice Provider
On server node, go to the node
'Webservices (JAX-WS RI 2.1.6)' | 'Provider' | 'Services'. Right-click on that node to create a new webservice provider . In the first wizard step, choose the 1st option to create a webservice provider from scratch, meaning not based on an existing WSDL file . Click next (two times) to enter the WSDL name which is
CompanyWebService.wsdl. Also, change the name for the service operation to
getCompanies . Click Finish to create the webservice provider. Click here to learn more about webservice provider creation. In case you encounter problems to build the webservice stub, refer to the Console Output for more information.
Please find the created webservice provider
CompanyWebService in Scout Explorer . In the Scout Property View of this provider, you can access the various files such as the WSDL file, implementation class or the JAX-WS service type. Also, there you find links to rebuild the webservice stub, to change build properties for stub generation process, to edit binding customization files and more. Also, the authentication mechanism can be changed in the section
Authentication and session context. For more information on that Property View, please click here.
So far, our webservice consists of a single operation to return companies. To open the WSDL file in the WSDL editor, click on the link 'CompanyWebService.wsdl' in the Scout Property View , To open the implementing port type, double click the webservice node or click on the 'CompanyWebService' link in the PropertyView .
As you can see, this webservice consists of a single operation
getCompanies with a
String as its return value and a
String as its parameter. We will change that to return a list of company objects with no input parameter. Because this webservice is a Contract-First webservice, meaning that we are designing the WSDL file by ourself rather than generating it based on Java classes, open the WSDL editor  to change the service. Click here to follow detailed instructions. Alternatively, you can download the changed WSDL file from here.
As you changed the WSDL file, you have to regenerate the stub anew. Thereto, click on 'Rebuild webservice stub' in the Scout Property View of the provider . If the WSDL file is valid, the stub generation will succeed. Otherwise, please refer to the Console Output to see what went wrong. Because we changed the method
getCompanies, compilation errors are shown in the
CompanyWebService Port Type . Remove this wrong
getCompanies method and add the unimplemented method instead . As you can see, the method returns a list of companies. That is exactely what we wanted to have.
Finally, you have to implement the method
getCompanies accordingly. Here, we simply return a list of all companies in database. Click here to get the source.
One last thing we still have to do. Because we configured the webservice to have authentication installed and to validate the request's credentials against config.ini , we have to specify the list of valid credentials in config.ini. Thereto, add the following line to the config.ini: That allows the user
eclipse/scout to access our webservice.
Log Webservice Providers's SOAP messages
To log the SOAP requests and response, click on
'Webservices (JAX-WS RI 2.1.6)' | 'Provider' | 'Services' | 'CompanyWebService' | 'Handler Registration' . Click on the link 'Add handler chain' to create a handler chain and 'Add handler' to add a handler to the chain . By clicking on the 'Browse' button, choose 'DatabaseLogHandler' as the log handler.
Test the Webservice Provider
Launch the server and open your browser. Enter the URL http://localhost:8080/tutorial/jaxws/ and you get a list of all your installed webservice providers.
This looks as follows:
To test your webservice implementation, download a webservice testing tool such as SoapUI.
In SoapUI, create a new 'SoapUI project' and enter http://localhost:8080/tutorial/jaxws/CompanyWebService?wsdl as your initial WSDL file. By double-clicking on the 'Request 1' node, the request dialog opens. On the property page, enter 'eclipse' as username and 'scout' as password. By clicking on the green 'play' button, the request is sent to your webservice and the SOAP response appears on the right side.