cornercorner
FeaturesPluginsDocs & SupportCommunityPartners

NB6JAXBSample1

JAXB and Provider API based JAX-WS Web Services.

Java Service Endpoint Interface (SEI) artifacts and stub implementation by Netbeans JAX-WS Wizard gives high level Java centric model for developers.

However there are times when one wants to operate on XML messages level. This is especially the case in Web Services, Application integration and Business integration applications. JAX-WS Provider API provides one to operate at the XML message level in a Web service.

Below example shows one such an example using simple Credit Report Web Service. Later we will show how to use JAXB binding file to customize generated classes.

Credit Report Web Service will consolidate credit report from different sources and inputs in different format.

Softwares needed


Steps

We will follow below steps to create our sample Web Service.

  • Create an EJB project using Java EE 5 version.
  • Create a JAXB Binding.
  • Copy the WSDL files to be packaged with jar file.
  • Create Provider API implementation class
  • Copy sample XML files to simulate different XML sources.
  • Build and deploy the EJB application to Glassfish.
  • Use your favorite Web Services client to test the Web Service.
  • Customizing generated classes: Importing and editing binding file.

Create an EJB project of Java EE 5 version.


File -> New project

EjbProject_NB6JAXBSample1.PNG

Create a JAXB Binding.

On Project node, New -> JAXB Binding
or New -> Other -> XML -> JAXB Binding

Image:CreateJAXBBinding1_NB6JAXBSample1.png


Enter Binding name, Select WSDL which imports Schema file , click finish.
(You need to copy/download both WSDL and Schema files to the same location)

Image:CreateJAXBBinding2_NB6JAXBSample1.png


You will notice JAXB binding node being created and compiled.

Image:CreateJAXBBinding3_NB6JAXBSample1.png


Also notice in File view generated Java classes in build/generated/addons/jaxb/

Image:CreateJAXBBinding4_NB6JAXBSample1.png

Copy the WSDL files to be packaged with jar file.

Select File view tab on Project explorer. Copy the 'conf/xml-resources/jaxb/CreditCoReportSchema' directory 'conf/wsdl' directory. So that WSDL and Schema files are packaged during Ejb Jar build.

Image:CopySchemaFiles_NB6JAXBSample1.png

Create Provider API implementation class

Create/copy the 'CreditCoReportProvider' class. CreditCoReportProvider_NB6JAXBSample1.java

Image:CreateProviderAPIImpl_NB6JAXBSample1.png

Invoke method in ProviderImpl consolidates data from different sources using methods getCreditInfo, getEmploymentInfo etc.

Image:ProviderImpl1_NB6JAXBSample1.png

Method getCreditEnquiries and getCreditInfo returns JAXB generated objects created differently. One uses XML source/InputStream other directly instantiates and populates the content.

Image:ProviderImpl2_NB6JAXBSample1.png

Copy sample XML files to simulate different XML sources.

Copy the 'addresses_NB6JAXBSample1.xml', 'creditEnquiries_NB6JAXBSample1.xml' and 'employmentInfo_NB6JAXBSample1.xml' files to 'com.creditco.ws.report' package. As we use these to simulate XML data received from other applications.

Image:createXMLDataFiles_NB6JAXBSample1.png

Build and deploy the EJB application to Glassfish.

Build and deploy the project to Glassfish 9.1 application server. Note the endpoint address.

Image:BuildDeploy1_NB6JAXBSample1.png

Use your favorite Web Services client to test the Web Service.

Use your favorite Web Services client. Suffix '?WSDL' to get URL for the Webservice WSDL just deployed.

Image:WSTest1_NB6JAXBSample1.png

Image:WSTest2_NB6JAXBSample1.png

Customizing generated classes: Importing and editing binding file.

JAXB allows developers to customize generation of Java classes.

In our example, if we want to change the generated class name of 'CreditAccount', we could do the following to change it.

Select the JAXB node 'CreditCoReportSchema' and action 'Change JAXB Options' .



Image:ChangeJAXBOptions1_NB6JAXBSample1.png

Import binding file 'CreditCoCustomization_NB6JAXBSample1.xjb'

Image:ChangeJAXBOptions2_NB6JAXBSample1.png

Double click on 'CreditCoCustomization_NB6JAXBSample1.xjb.xml' to edit the binding file. Change the class name for element 'CreditAccount' to 'CreditCardAccount' . Click on 'JAXB bindings' node and select 'Regenerate Java Code' action.

You will see editor showing invalid class reference for 'CreditAccount' in CreditCoReportProvider_NB6JAXBSample1.java file.

Replace class reference 'CreditAccount' with 'CreditCardAccount' in CreditCoReportProvider_NB6JAXBSample1.java to use new name.

Image:ChangeJAXBBindingFile1_NB6JAXBSample1.png