O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Header References with Holder<T>

Problem

You want to define an operation that requires SOAP headers and invoke it from a client.

Solution

In the service, use the @WebParam annotation with your operation parameter. Specify header=true, and wrap your parameter in a Holder<T>, where T is the actual type of the parameter you want. On the client, use a Holder<T> to set the values of the parameter elements and get the return values with a reference.

Discussion

The following examples illustrate how to use a Holder to handle the SOAP headers required for using a web service. This is a command-line client for the web service, and it is totally portable; it doesn’t rely on proprietary extensions within Glassfish or another container. The web service and WSDL are shown as well to help aid your understanding the process.

First, you’ll create a web service using the “Start from Java” approach. This is easy to do, and lets you generate a WSDL with wsgen that indicates the required header values. Most containers will automatically generate a WSDL for you on deployment, so all you should need to do is create the Java class as part of a WAR project within your favorite IDE, and package the class in WEB-INF/classes as you would any other regular Java class. The web service is shown in Example 7-19.

Example 7-19. Email verifier web service requiring credentials in SOAP headers

package com.soacookbook; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.ws.Holder; /* * Very simple ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required