Java Reference
In-Depth Information
Adding Headers to a SOAP Request
Problem
You want to add custom header information to a SOAP request.
Solution
Use the SOAPEnvelop.addHeader method.
Discussion
The SOAP 1.2 specification covers headers at length, and they are a popular choice for storing
all manners of data concerning message exchanges within a SOA. There are also a number of
SOAP headers that can be defined to comply with the specification when you need to use cer-
tain kinds of behavior. Some of the headers defined by the specification include mustUnder-
stand , role , and relay . So, it's important to understand how to use them.
Any user-defined header can also be defined by service clients and providers. Headers can be
used to indicate vendor extensions (typically beginning with “X-”), or processing instructions
such as what parts of an infoset can be cached, security codes, or other bits of meta-data.
There are a few basic ways to do work with SOAP headers. You can use the SAAJ API, which
ensures portability, and add the headers to your code on invocation. That's what we'll exam-
ine here.
In this example, you'll create a web service, examine the schema and WSDL that define it,
and then write the client program to contact it. We'll walk through every step to make sure
that it's clear how all of the parts get wired together.
Creating the web service
The first thing you'll do is create a web service. It will specify some header value that it wants
clients to provide as a SOAP header, which it can then read on the server side and perform
some processing on it. You'll be creating a passwordHeader header. (You won't actually do
any security stuff with it because that's not the point here, but you could.) The service itself is
kept as simple as possible in order to stay focused on the task at hand. It just adds two integers
together.
In Example 5-9 , you see the web service you've defined that requires the headers. You'll cre-
ate this quickly using JAX-WS. This will give you something to test against.
Search WWH ::




Custom Search