Chapter 5. Invoking Web Services

Table of Contents

5.1. Overview
5.2. Web Service Invocation Using Dynamic Proxy
5.2.1. Creating Client Artifacts
5.2.2. Java SE Client Invocation
5.2.3. Java EE Client Invocation
5.3. Web Service Invocation Using Dispatch Method

This chapter describes how to invoke web services with implementation examples.

A JAX-WS web service client application can access a remote web service endpoint through the following methods.

There are two kinds of web service invocation through a dynamic proxy, Java SE and Java EE. Client artifacts need to be created beforehand for both types.

This section describes about the Java SE and Java EE web service invocation methods, and how to create the client artifacts.

Whether a web service is generated from a Java class or WSDL file, the web service client is invoked by creating a Java class after creating the client artifacts with the published WSDL file. This section briefly shows how to create the client artifacts for a client invoking the web service created by the Java class from the previous chapter.

Web service client artifacts (proxies) consist of service endpoint interface and service interface classes, which were obtained from the WSDL provided by the service by using the JEUS 7 wsimport tool.

The following example shows how to obtain the client artifacts by using wsimport in the console.

$ wsimport -help
Usage: wsimport [options] <WSDL_URI>

where [options] include:
  -b <path>                 specify external jaxws or jaxb binding
                            files (Each <file> must have its own -b)
  -catalog <file>           specify catalog file to resolve external
                            entity references supports TR9401,
                            XCatalog, and OASIS XML Catalog format.
  -d <directory>            specify where to place generated output files
  -extension                allow vendor extensions - functionality
                            not specified by the specification. Use
                            of extensions may result in applications
                            that are not portable or may not
                            interoperate with other implementations
  -help                     display help
  -httpproxy:<host>:<port>  specify a HTTP proxy server (port defaults to 8080)
  -keep                     keep generated files
  -p <pkg>                  specifies the target package
  -quiet                    suppress wsimport output
  -s <directory>            specify where to place generated source files
  -target <version>         generate code as per the given JAXWS
                            specification version. version 2.0 will
                            generate compliant code for JAXWS 2.0 spec.
  -verbose                  output messages about what the compiler is doing
  -version                  print version information
  -wsdllocation <location>  @WebServiceClient.wsdlLocation value

Examples:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl

Portable artifacts are created by using wsimport ANT TASK with a WSDL file of the following remote web service.



The following are the files that are generated as the result of creating the portable artifacts from the WSDL file of the web service which will be invoked using the aforementioned wsimport ANT TASK.

fromwsdl/client/AddNumbers.class
fromwsdl/client/AddNumbersImpl.class
fromwsdl/client/AddNumbersImplService.class
fromwsdl/client/AddNumbersResponse.class
fromwsdl/client/ObjectFactory.class
fromwsdl/client/package-info.class

JAXB uses the java bean files, AddNumbers and AddNumbersResponse, to convert a request and response, respectively, for the addNumbers method.

The AddNumbersImpl file implements the service endpoint interface, and the AddNumbersImplService file implements the service interface Java class used as a proxy by the client. The ObjectFactory and packageinfo classes are created by JAXB.

The following is the AddNumbersImplService class which is the service interface class obtained from the WSDL file of the previous remote web service by using wsimport.

[Example 5.3] << AddNumbersImplService.java >>

@WebServiceClient(name = "AddNumbersImplService",
    targetNamespace = "http://server.fromjava/",
    wsdlLocation = "http://localhost:8088/AddNumbers/AddNumbersImplService?wsdl")
public class AddNumbersImplService extends Service {

    private final static URL ADDNUMBERSIMPLSERVICE_WSDL_LOCATION;
    private final static WebServiceException ADDNUMBERSIMPLSERVICE_EXCEPTION;
    private final static QName ADDNUMBERSIMPLSERVICE_QNAME =
            new QName("http://server.fromjava/", "AddNumbersImplService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL(
                "http://localhost:8088/AddNumbers/AddNumbersImplService?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        ADDNUMBERSIMPLSERVICE_WSDL_LOCATION = url;
        ADDNUMBERSIMPLSERVICE_EXCEPTION = e;
    }

    public AddNumbersImplService() {
        super(__getWsdlLocation(), ADDNUMBERSIMPLSERVICE_QNAME);
    }

    public AddNumbersImplService(WebServiceFeature... features) {
        super(__getWsdlLocation(), ADDNUMBERSIMPLSERVICE_QNAME, features);
    }

    public AddNumbersImplService(URL wsdlLocation) {
        super(wsdlLocation, ADDNUMBERSIMPLSERVICE_QNAME);
    }

    public AddNumbersImplService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, ADDNUMBERSIMPLSERVICE_QNAME, features);
    }

    public AddNumbersImplService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public AddNumbersImplService(URL wsdlLocation, QName serviceName,
                                 WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    @WebEndpoint(name = "AddNumbersImplPort")
    public AddNumbersImpl getAddNumbersImplPort() {
        return super.getPort(new QName("http://server.fromjava/", 
                                       "AddNumbersImplPort"),
                             AddNumbersImpl.class);
    }

    @WebEndpoint(name = "AddNumbersImplPort")
    public AddNumbersImpl getAddNumbersImplPort(WebServiceFeature... features) {
      return super.getPort(new QName("http://server.fromjava/", 
                                     "AddNumbersImplPort"),
                            AddNumbersImpl.class, features);
    }

    private static URL __getWsdlLocation() {
        if (ADDNUMBERSIMPLSERVICE_EXCEPTION!= null) {
            throw ADDNUMBERSIMPLSERVICE_EXCEPTION;
        }
        return ADDNUMBERSIMPLSERVICE_WSDL_LOCATION;
    }
}


The previous service interface is used to obtain the actual proxy object from the client.

The client can obtain the service endpoint interface AddNumbersImpl by using the getAddNumbersImplPort() method of the AddNumbersImplService object, which was instantiated through the AddNumbersImplService constructor of the previous example class.

The following is the AddNumbersImpl class which is a service endpoint interface obtained from the WSDL file of the previous remote web service by using wsimport.


The service endpoint interface has annotations for converting into Java object, which is used for dynamic binding or at runtime, or XML document. (The service endpoint interface may be used to recreate the WSDL and schema files, but they may not be identical to the WSDL or schema files used to obtain the service endpoint interface.)

The following are Java bean classes, AddNumbers and AddNumbersResponse, used by JAXB to convert a request and response of the addNumbers method into a Java object or XML document.



The two Java bean classes, AddNumbers and AddNumbersResponse, are message elements in the WSDL file of the remote web service.

This section describes how to create and invoke a client class, that contains the logic for invoking a remote web service, by using client portable artifacts obtained in the "5.2.1. Creating Client Artifacts" section.

This section explains how to use the Java EE method to create and invoke a client that calls a web service generated through the WSDL file from the previous section. It is assumed that the client portable artifacts have already been obtained as stated in the "5.2.1. Creating Client Artifacts" section.

In generating Java EE clients for a JAX-RPC web service, the <service-ref> element is added to the web.xml file of a servlet or the ejb-jar.xml file of an EJB. This way, the information required to create a client proxy for a web service gets registered with the JNDI. However, the @WebServiceRef annotation needs to be configured to generate Java EE clients for a JAX-WS web service.

The following example shows how to configure the @WebServiceRef Annotation.

...
@WebServiceRef(wsdlLocation="http://host:port/TmaxService/TmaxService?wsdl)
static TmaxServiceImplService tsvc;
...

Declare a client Java class member variable with the @WebServiceRef annotation by using the AddNumbersService interface. The member variable will automatically inject its value into a servlet container for a servlet or into an EJB container for an EJB, even if there is no set method when the client class is initialized at runtime.

By doing this, AddNumbersPortType, an object which implements the service endpoint interface, can be obtained. The object contains logic for invoking a remote web service through a dynamic proxy. This object can be used to call the method that invokes the actual web service.

The following shows a client program that invokes a remote web service by using portable artifacts obtained through the wsimport ANT TASK execution in the previous example.


Invoking web services by using the dispatch method is intended for developers who prefer handling the XML configuration at the XML level by using the java.lang.transform.Source or javax.xml.soap.SOAPMessage interfaces. Web service invocation using the dispatch method can be done in the message or payload mode, and can be used to create REST web services through XML/HTTP binding (javax.activation.DataSource).

For more information, refer to "Chapter 8. Provider and Dispatch Interfaces".