Sun Glassfish Enterprise Server 

Samples Main Page

The Hello-WebServiceRef Sample Application

This JAX-WS Web Services sample demonstrates the injecton of Web Service Client by the container through the use of @WebServiceRef and configuring WebService features like MTOM, Adddressing on such injected resource.
This sample also shows the use of @WebServiceRef in WebClient and the JavaEE container will inject any resources annotated as WebService references.

Web Service

package enterprise.hello_webservice;

import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import javax.xml.ws.soap.Addressing;
import javax.xml.ws.WebServiceException;

@MTOM
@WebService
public class Hello
{
    public String getHello(String name)
    {
        return "Hello " + name + "!";
    }

    public byte[] getHelloBinary(byte[] name) {
        try {
        String nameStr = new String(name,"UTF-8");       
        return ("Hello " + nameStr + "!").getBytes("UTF-8");
        } catch(java.io.UnsupportedEncodingException ex) {
          throw new WebServiceException(ex);
        }

    }
}

This sample WebServcie is configured with MTOM feature through @MTOM. See Metro guide for more details on MTOM feature

Deployment We create a simple war and deploy it to v3. No wsgen is used to generate artifacts as JAXWS 2.2 dynamically generates all the artifacts.

You can verify the Webservice is active by entering this in a browser (example server host and port):

http://localhost:8080/hello-webserviceref/HelloService?wsdl

Getting the Artifact files

In order to compile a client, we need some special files that the Hello Webservice will give us. This is required so that the client has the webservice interfaces and ports available. We use wsimport, an App Server-supplied program to do this like so:

${javaee.home}/bin/wsimport -Xendorsed -package=enterprise.hello_webserviceref_client -d ./build http://${javaee.server.name}:${javaee.server.port}/hello-webserviceref/HelloService?wsdl 
Note: the Xendorsed option since JDK 6 u4 JAXWS 2.1 is packaged as part of rt.jar. We want to use JAXWS 2.2 apis and need to override the JAXWS 2.1 which is part of JDK 6u4 so we need to use Xendorsed"

Compiling the Client

The Client is also simple:

import javax.xml.ws.WebServiceRef;
import javax.xml.ws.soap.MTOM;

import enterprise.hello_webserviceref_client.HelloService;
import enterprise.hello_webserviceref_client.Hello;

public class WebServiceRef_AppClient {
    @MTOM
        @javax.xml.ws.WebServiceRef(HelloService.class)
        static Hello port;

    public static void main(String[] args) {
        WebServiceRef_AppClient client = new WebServiceRef_AppClient();
        client.doHelloBinary();
    }

    public void doHelloBinary() {
        try {
            //HelloService service = new HelloService();
            //Hello port = service.getHelloPort();
            byte[] ret = port.getHelloBinary(System.getProperty("user.name").getBytes("UTF-8"));
            System.out.println("Hello result = " + new String(ret,"UTF-8"));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Note: the use of @WebServiceRef and configuring MTOMFeature on the WebService client, which will be injected by the JavaEE container.

Running the Client

You need to run the client using appclient, See the build.xml for more information

The client will print out a greeting from the Webservice using your name.

Building, Deploying, and Running the Application

Perform the following steps to build, deploy, and run the application:

  1. Set up your build environment and configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee6/webservices/hello-jaxws2.2.
  3. Change directory to app_dir.
  4. Build, deploy, and run the sample application using the all target.
  5. app_dir> ant all

  6. Use the target clean to undeploy the sample application and to remove the temporary directories such as build and dist.

    app_dir> ant clean

Building, Deploying, and Running the Application in NetBeans IDE

Perform the following steps to build, deploy, and run the application using NetBeans IDE:

  1. Refer to the common build instructions for setting up NetBeans IDE and Sun GlassFish Enterprise Server.
  2. In the NetBeans IDE, choose File → Open Project (Ctrl-Shift-O), navigate to the samples_install_dir/javaee6/webservices/ directory, select hello-webserviceref, and click Open Project.
  3. In the Projects tab, right click hello-webserviceref and select Run to build, deploy, and run the project.

Troubleshooting

If you have problems when running the application, refer the troubleshooting document.


Copyright © 2009 Sun Microsystems, Inc. All rights reserved.