You are currently viewing Sun JAX-WS: Simple Web Service

Sun JAX-WS: Simple Web Service

Please, use eclipse IDE and follow the steps for the following simple web service,
which returns the OS name from the server on which it is hosted.
Eclipse IDE wizards create web services with apache-axis libraries,
however in the following example we will use the sun-jaxws libraries:

1. Create a java project GetSystemInfoWS
packages are:
com.vag.server, which includes the WebService
com.vag.client, which includes the local class for calling this WS

2. Add a dynamic web project facet from Project Properties (Project Facets).
The reason why add dynamic web project attributes, is in to be able to define the .war package, without much effort.
Otherwise we would create it manually and run the command wsgen:

3. Add the server interface and implementation java classes, on which the web service will be based:

GetSystemInfoServer.java:

package com.vag.server;
//Service Endpoint Interface for the GetSystemInfoServer (SEI)

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public interface GetSystemInfoServer {
@WebMethod
String getSystemInfoAsString();
}

GetSystemInfoServerImpl.java:

package com.vag.server;
//Service Implementation Bean for the GetSystemInfoServer

import javax.jws.WebService;

@WebService(endpointInterface="com.vag.server.GetSystemInfoServer")
public class GetSystemInfoServerImpl implements GetSystemInfoServer {
public String getSystemInfoAsString() {
return System.getProperty("os.name").toString();
}
}

4. Add the client classes, which will call (test) this service
(The reason why we put this client in the same project, is because we need the libraries for the ws call.
We could make a separate project, but in this case we should reference the existing WS project.
So, it is more simple to include the client classes in the same project, although it seems more complicated)


GetSystemInfoClientWeblogic.java:

package com.vag.client;
//Java Client for the Java Web Service

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.vag.server.GetSystemInfoServer;
import java.net.URL;

class GetSystemInfoClientWeblogic {
    public static void main(String args[]) throws Exception {
        URL url = new URL("http://myhost:7001/GetSystemInfo/si?wsdl");

        
        //Qualified name of the service:
        //1st arg is the service URI
        //2nd arg is the service name published in the WSDL
        //ATTENTION: Reverse Order!!!
        QName qname = new QName("http://server.vag.com/", "GetSystemInfoServerImplService");

        
        //Create a factory for the service
        Service service = Service.create(url, qname);
        

        //Extract the endpoint interface, the service "port"
        GetSystemInfoServer eif = service.getPort(GetSystemInfoServer.class);
        System.out.println(eif.getSystemInfoAsString());    
    }
}

5. Put the following files into the “Java Build Path”:

JAVA_TOP\frameworks\sun-webservices

    – jaxb-impl-2.1.8.jar
    – jaxws-rt-2.1.4.jar
    – stax-ex-1.2.jar
    – streambuffer-0.9.jar
   
The same classes put also in the Properties > Deployment Assembly

6. Create the sun-jaws.xml, which defines the endpoint of the web-service:

\WEB-INF\sun-jaxws.xml:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">

    <endpoint
        name="GetSystemInfoWS"
        implementation="com.vag.server.GetSystemInfoServerImpl"
        url-pattern="/si" />
        
</endpoints>

[endpoint:    name: GetSystemInfoWS]

7. Create the web.xml, which will define the servlet and the listener:
(web.xml is needed in all web projects)

\WEB-INF\web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
                        version="2.4">
                
    <listener>
        <listener-class>
            com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>GetSystemInfoWS</servlet-name>
        <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>GetSystemInfoWS</servlet-name>
        <url-pattern>/si</url-pattern>
    </servlet-mapping>

</web-app>

[listener is:         com.sun.xml.ws.transport.http.servlet.WSServletContextListener]
[servlet-class is:     com.sun.xml.ws.transport.http.servlet.WSServlet]
[servlet:             servlet-name: GetSystemInfoWS]
[servlet-mapping:     servlet-name: GetSystemInfoWS]

7. Create the .war file: C:\GetSystemInfoWS.war (export > WAR file)

8. Deploy to Tomcat or to WebLogic (or JBoss, Tomcat etc)

9. To test the deployment of .war file, type this address on the browser:

http://myhost:7001/GetSystemInfo/si?wsdl

<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5. -->
<definitions targetNamespace="http://server.vag.com/"
    name="GetSystemInfoServerImplService">
    <types />
    <message name="getSystemInfoAsString" />
    <message name="getSystemInfoAsStringResponse">
        <part name="return" type="xsd:string" />
    </message>
    <portType name="GetSystemInfoServer">
        <operation name="getSystemInfoAsString">
            <input message="tns:getSystemInfoAsString" />
            <output message="tns:getSystemInfoAsStringResponse" />
        </operation>
    </portType>
    <binding name="GetSystemInfoServerImplPortBinding" type="tns:GetSystemInfoServer">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
            style="rpc" />
        <operation name="getSystemInfoAsString">
            <soap:operation soapAction="" />
            <input>
                <soap:body use="literal" namespace="http://server.vag.com/" />
            </input>
            <output>
                <soap:body use="literal" namespace="http://server.vag.com/" />
            </output>
        </operation>
    </binding>
    <service name="GetSystemInfoServerImplService">
        <port name="GetSystemInfoServerImplPort" binding="tns:GetSystemInfoServerImplPortBinding">
            <soap:address location="http://myhost:7001/GetSystemInfo/si" />
        </port>
    </service>
</definitions>

10. Call Service to Weblogic, by right click, run as java application on:

GetSystemInfoClientWeblogic.java

The file hierarchy in project is shown here:

Project structure

and the file hierarchy in the WAR file is shown here (don’t forget the libraries!):

WAR structure

Folder wsdl with the wsdl file in WAR structure is optional.

However, it can be added selecting New > WSDL from file menu,

which will take as parameter (select RPC style):

http://myhost:7001/GetSystemInfo/si?wsdl

Alternatively, it can be created by command line with wsgen utility:

cd <project_top>\bin
%JAVA_HOME%\bin\wsgen -wsdl -d WEB-INF\wsdl -cp . com.vag.server.GetSystemInfoServerImpl

Also, com.vag.client classes are not required in WAR file.

To pack the WEB-INF directory in .war file through command line:

jar cvf GetSystemInfo.war WEB-INF

Leave a Reply