Jun 19, 2011

Java Use Bottom-Up Method and JAX-WS to Develop Web Service Provider



Web services can be created using two methods: top-down and bottom-up. Bottom-up Web services development involves creating a Web service from a Java™ bean or enterprise bean.

When creating a Web service using a top-down approach, first you design the implementation of the Web service by creating a WSDL file. You can do this using the WSDL Editor. You can then use the Web services wizard to create the Web service and skeleton Java™ classes to which you can add the required code.

Although bottom-up Web service development may be faster and easier, especially if you are new to Web services, the top-down approach is the recommended way of creating a Web service. By creating the WSDL file first you will ultimately have more control over the Web service, and can eliminate interoperability issues that may arise when creating a Web service using the bottom-up method.

Although bottom-up Web service development may be faster and easier, especially if you are new to Web services, the top-down approach is the recommended way of creating a Web service.

When creating a Web service using a bottom-up approach, first you create a Java bean or EJB bean and then use the Web services wizard to create the WSDL file and Web service.

Reference: https://publib.boulder.ibm.com/infocenter/ratdevz/v8r0/index.jsp?topic=/org.eclipse.jst.ws.doc.user/concepts/cwsbtmup.html

Reference: https://publib.boulder.ibm.com/infocenter/ratdevz/v8r0/index.jsp?topic=/org.eclipse.jst.ws.doc.user/concepts/cwsbtmup.html

Develop Web Service should have a knowledge about XML Schema and Web Services Description Language. You can learn those at W3Schools:

XML Schema: http://www.w3schools.com/schema/default.asp
Web Services Description Language: http://www.w3schools.com/WSDL/default.asp

This tutorial is use bottom-up to develop Web Service to let you have a basic concept, because use bottom-up method develop is easy than use top-down method. Please create a WAR project and add a Java class:
package sample.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(
        name = "CalculaterService", 
        serviceName = "CalculaterService", 
        portName = "CalculaterServicePort", 
        targetNamespace = "http://www.memorylack.com/CalculaterService/")
public class CalculaterService {

    @WebMethod
    public int add(int i, int k) {
        return i + k;
    }

    @WebMethod
    public int subtract(int i, int k) {
        return i - k;
    }

}

You have create a simple Web Service with a Java bean.

@WebService(
        name = "CalculaterService", 
        serviceName = "CalculaterService", 
        portName = "CalculaterServicePort", 
        targetNamespace = "http://www.memorylack.com/CalculaterService/")

This annotation is a marker to notify Java EE container this is a Web Service class.

  • name: display on application server
  • serviceName: display in URL and WSDL
  • portName: display in WSDL
  • targetNamespace : XML namespace, display in WSDL and XSD

@WebMethod

This annotation is a marker to notify Java EE container this is a Web Service function.

CalculaterService also is a EJB session bean:

package sample.ws;

@Local
public interface CalculaterService{

    public int add(int i, int k);
    public int subtract(int i, int k);

}

package sample.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@Stateless
@WebService(
        name = "CalculaterService", 
        serviceName = "CalculaterService", 
        portName = "CalculaterServicePort", 
        targetNamespace = "http://www.memorylack.com/CalculaterService/")
public class CalculaterServiceBean implements CalculaterService{

    @WebMethod
    public int add(int i, int k) {
        return i + k;
    }

    @WebMethod
    public int subtract(int i, int k) {
        return i - k;
    }

}

Deploy this project to the application server and use browser to open http://localhost/CalculaterService?wsdl. Browser will display WSDL content same as below:
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="CalculaterService" targetNamespace="http://www.memorylack.com/CalculaterService/"
    xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.memorylack.com/CalculaterService/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

    <types>
        <xsd:schema>
            <xsd:import namespace="http://www.memorylack.com/CalculaterService/"
                schemaLocation="CalculaterService_schema1.xsd" />
        </xsd:schema>
    </types>

    <message name="addResponse">
        <part name="parameters" element="tns:addResponse">
        </part>
    </message>

    <message name="add">
        <part name="parameters" element="tns:add">
        </part>
    </message>

    <message name="subtractResponse">
        <part name="parameters" element="tns:subtractResponse">
        </part>
    </message>

    <message name="subtract">
        <part name="parameters" element="tns:subtract">
        </part>
    </message>

    <portType name="CalculaterService">
        <operation name="add">
            <input message="tns:add">
            </input>
            <output message="tns:addResponse">
            </output>
        </operation>

        <operation name="subtract">
            <input message="tns:subtract">
            </input>
            <output message="tns:subtractResponse">
            </output>
        </operation>
    </portType>

    <binding name="CalculaterServicePortBinding" type="tns:CalculaterService">
        <soap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />
        <operation name="add">
            <soap:operation soapAction="" />
            <input>
                <soap:body use="literal" />
            </input>
            <output>
                <soap:body use="literal" />
            </output>
        </operation>
        <operation name="subtract">
            <soap:operation soapAction="" />
            <input>
                <soap:body use="literal" />
            </input>
            <output>
                <soap:body use="literal" />
            </output>
        </operation>
    </binding>

    <service name="CalculaterService">
        <port name="CalculaterServicePort" binding="tns:CalculaterServicePortBinding">
            <soap:address
                location="http://localhost/CalculaterService" />
        </port>
    </service>
</definitions>

Application server generate WSDL file, also generate XSD file. Use browser open http://localhost/CalculaterService/CalculaterService_schema1.xsd, browser display content same as below:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.memorylack.com/CalculaterService/" version="1.0"
    targetNamespace="http://www.memorylack.com/CalculaterService/">

    <xs:element name="add" type="tns:add"></xs:element>
    <xs:element name="addResponse" type="tns:addResponse"></xs:element>
    <xs:element name="subtract" type="tns:subtract"></xs:element>
    <xs:element name="subtractResponse" type="tns:subtractResponse"></xs:element>

    <xs:complexType name="add">
        <xs:sequence>
            <xs:element name="arg0" type="xs:int"></xs:element>
            <xs:element name="arg1" type="xs:int"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="addResponse">
        <xs:sequence>
            <xs:element name="return" type="xs:int"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="subtract">
        <xs:sequence>
            <xs:element name="arg0" type="xs:int"></xs:element>
            <xs:element name="arg1" type="xs:int"></xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="subtractResponse">
        <xs:sequence>
            <xs:element name="return" type="xs:int"></xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

If you can view WSDL and XSD, you are finish to deploy Web Service on application server. We can use soapUI to simulate Web Service Client.

Download address: http://sourceforge.net/projects/soapui/files/

Please select installable file to download. When you installed sopaUI, please follow below step to simulate Web Service Client.

  1. File -> New soapUI Project
  2. Project name enter localhost and click OK
  3. Project + mouse right clicke -> Add WSDL
  4. WSDL Location enter http://localhost/CalculaterService/CalculaterService.wsdl

Now, you can see 2 web service function name in the tree. Open add function and open Request 1. Left window will display below XML content:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ws="http://www.memorylack.com/CalculaterService/">
    <soapenv:Header />
    <soapenv:Body>
        <ws:add>
            <arg0>?</arg0>
            <arg1>?</arg1>
        </ws:add>
    </soapenv:Body>
</soapenv:Envelope>

Modify arg0 and arg1:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ws="http://www.memorylack.com/CalculaterService/">
    <soapenv:Header />
    <soapenv:Body>
        <ws:add>
            <arg0>1</arg0>
            <arg1>2</arg1>
        </ws:add>
    </soapenv:Body>
</soapenv:Envelope>

Send to application server. Application server should be response below XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <dlwmin:addResponse xmlns:dlwmin="http://www.memorylack.com/CalculaterService/">
            <return>3</return>
        </dlwmin:addResponse>
    </soapenv:Body>
</soapenv:Envelope>

However, web service can be input and return Object type:
Object Class:
package sample.ws;

public class Info{

    private String firstName;
    private String lastName;

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    public void setLastName(String lastName){
        this.lastName = lastName;
    }

}

Web Service:
@WebMethod
public String sayHello(Info info) {
    return "Hello! " + info.getFirstName() 
            + " " + info.getLastName();
}

@WebMethod
public Info getInfo(int id){
    return findInfo(id);
}

Next post: Java Use Top Down Method and JAX-WS to Develop Web Service Provider


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Google Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Hostgator Coupon Code