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

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 certain kinds of behavior. Some of the headers defined by the specification include mustUnderstand, 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 examine 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 ...

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