Joost Hofman, GlassFish v2 | Tags: , , , ,

Often an XSD is supplied by an external or internal employee with purpose to create a Web service.

In this article I will step by step explain how to create a webservice with merly a XSD.

Step 1: Create an EJB module

Click File> new project, then select the group “Java EE ” and the option EJB module.
In this example we will set up an authentication service.
We will use the name Authentication Service (fig1).

fig1.
xsdToWebservice1

click next and finish.

Stap 1.5: create a XSD

Because this is an example we will create the XSD ourselves.
Right Mouse click on the project> new> other.
Now select from the group “XML” the option XML schema.

We will give the name “authenticationSchema” and give an appropriate target namespace. (fig2)

fig2.

xsdToWebservice - Create XSD

Open the schema in the editor.
We now have 3 different ways to edit the schema: Source, schema or design mode.
In this example we will edit the schema on design level.
Make 2 complextypes with the names, authenticationInputType and authenticationOutputType.
We give authenticatieIputType a username and password each element of type string.
For the authenticationOutputType, we create a username and valid element, where username is of type string and validate of type boolean.
Under elements we create an authenticationInput and authenticatieOuput element. Here we give the element authentiecatieInput the type of authenticationInputType and the element authenticationOuput the type of authenticationOutputType. (fig3)

fig3.

xsdtowebservice-showxsd

Step 2: Create a JAXB Binding

Now we can create a JAXB Binding!
A very strong option in Netbeans where you can quickly and easily implement JAXB.

Right Mouse click on the project> new> other.
Under the group “XML” select the option JAXB binding.

Since we are binding an authentication schema we will give the binding name; authenticationBinding.
At schema file browser we will browse to the schema on the file system.
After we selected the schema we give an appropriate package name. (fig4)

fig4.

xsdtowebservice2

In the project tree we now have an item: JAXB Bindings! (fig5)

fig5.

xsdtowebservice4

Step 3: Create a Webservice

We will now make a Webservice.
Again right click > new > other.
In the Webservices group select the Webservice item.
Give the Webservice an appropriate name, eg AuthenticationService.
Also use an appropriate name for the package. (fig6)

fig6.

xsdtowebservice3

Click on finish.

Step 4: Use the jaxb bindings as input and output

Now we come to the most important point!

Open the Webservice in the editor.
In Netbeans it is possible to edit the Webservice in design mode. This is what we will do.
Click Add Operation.
Give the operation a proper name and browse to the return type that we created in step 2.
Also add a parameter with the input type we created at step 2. (fig 7)

fig 7.

xsdtowebservice5

Click OK.

In the design mode you can now see a new operation. (fig 8)

fig 8.

xsdtowebservice6

We will now go into source view, to give our operation some simple code.
See fig 9 for an example.

fig9.

xsdtowebservice7

Step 5: Building And Deploy

Right mouse on the project > Deploy. When the local Glassfish server has not yet been launched, this will automatically happen and the Webservice is deployed.

We have in 5 simple steps without coding to much created a Webservice from an XSD!
Because the test Webpage in Glassfish not yet supports complextypes as input, we will create a webservice client and test our webservice in the next blog.



Leave a Reply