Saturday, May 15, 2010

Webservices using JAX-WS

Hello All,

We are going to create a simple webservice using JAX-WS.
This requires jdk 1.6, glassfish sever.
This is tested on Windows XP SP2, jdk1.6.0_18, Glassfish-v2ur1

This Webservices fetches information of a person by id.

Please follow the instructions...

• Create a directory structure like this.

 -----> PersonFetchService
             -----> work
             |            |
             |            |
             |            -----> personfetch.xsd
             |            |
             |            |
             |            -----> personfetch.xjb
             |            |
             |            |
             |            -----> compile.bat
             -----> PersonFetchApp
                         -----> WEB-INF
                                     -----> web.xml
                                     -----> classes

• personfetch.xsd

We have to determine the structure of request and response. What we want to send and what we want back, so we now define a xsd for that purpose.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="" targetNamespace="" elementFormDefault="qualified" xmlns:tns="">
    <xsd:complexType name="PersonFetchRequest">
            <xsd:element name="Id" type="xsd:int"/>
    <xsd:complexType name="PersonFetchResponse">
            <xsd:element name="Id" type="xsd:int"/>
            <xsd:element name="Name" type="xsd:string"/>
            <xsd:element name="Address" type="tns:Address"/>
            <xsd:element name="PhoneNo" type="xsd:decimal"/>
            <xsd:element name="Email" type="xsd:string"/>
            <xsd:element name="BirthDate" type="xsd:date"/>
    <xsd:complexType name="Address">
            <xsd:element name="Line1" type="xsd:string"/>
            <xsd:element name="Line2" type="xsd:string"/>
            <xsd:element name="Line3" type="xsd:string"/>
            <xsd:element name="City" type="xsd:string"/>
            <xsd:element name="Pincode" type="xsd:decimal"/>

• personfetch.xjb

The xjb file specifies in which package the generated java classes resides for given targetnamespace of xsd specified in schema location.

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="" xmlns:xsd="" version="1.0">
    <jaxb:bindings schemaLocation="personfetch.xsd" node="/xsd:schema">
            <jaxb:package name="com.samplews.personfetch"/>

• compile.bat

This batch is to compile all java file under specified package.

javac com/samplews/personfetch/*.java

• web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

• Navigate to "work" folder on command prompt.

• Execute this command.

This command generates java classes according to xsd and configuration of xjb which we will use to pass into and to return from webservice.

xjc personfetch.xsd -b personfetch.xjb -d .

You would get source files generated under "work".

• Create a java file under com --> samplews --> personfetch named "".

This is the webservice class. You can see the parameter and return type of the the fetchInfo method.


package com.samplews.personfetch;

import javax.jws.*;
import javax.jws.soap.SOAPBinding;
import java.math.BigDecimal;

@WebService(name = "PersonFetchWS", serviceName = "PersonFetchWSService", portName = "PersonFetchWSPort", targetNamespace = "")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)

public class PersonFetchWS {

    public PersonFetchWS() {


    @WebMethod(operationName = "FetchInfo")
    @WebResult(name = "PersonFetchResponse", targetNamespace = "")
    PersonFetchResponse fetchInfo(@WebParam(name = "PersonFetchRequest", targetNamespace = "")PersonFetchRequest request) {
        PersonFetchResponse response=new PersonFetchResponse();
        Address address=new Address();
        address.setPincode(new BigDecimal("387130"));
        response.setPhoneNo(new BigDecimal("9999999999"));
        return response;


• Execute the "compile.bat" to compile all source files.

• Copy the "com" folder and paste it to Webservices --> PersonFetchService --> PersonFetchApp --> WEB-INF --> classes

• Navidate to "PersonFetchService" on command prompt.

• Execute this command.

The command will package all stuff under "PersonFetchService" derectory and create a jar file.

jar cvf PersonFetchApp.war -C PersonFetchApp .

You would get "PersonFetchApp.war" under "PersonFetchService".

• Deploy this war file in glassfish.

When you deploy the war the glassfish internally uses wsgen to create server artifact like wsdl file, java file of request, java file of response, etc.

• Enter this url in browser's address bar.


• You would see Webservice information.

• To call the webservices use the class.



public class CallPersonFetchWS {

    public static void main(String args[]) throws Exception {
        URL url = new URL("http://[host]:[port]/PersonFetchApp/PersonFetchWSService");
        URLConnection con = url.openConnection();
        con.setRequestProperty("content-type", "text/xml");
        String request="<soap:Envelope xmlns:soap=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\" xmlns:ns1=\"\">"+
        byte[] filebytes = request.getBytes();
        OutputStream out = con.getOutputStream();
        try {
        } finally {
        InputStream in = con.getInputStream();
        byte[] inbytes = new byte[in.available()];
        try {
        } finally {
        String resStr = new String(inbytes);

I think this would help. Please put suggestions if this not helping you, so I can improve this.