Search code examples
javasoapheaderjax-ws

How to extract SOAP header information from a JAX-WS class?


I'm writing a JAX-WS WebService but I am running into a little stumbling block when it comes to extracting information from the SOAP header. My WebService class is annotated with @WebService and I am inject the WebServiceContext into the class with:

@Resource
private WebServiceContext webServiceContext;

but I'm a little stuck at this point as to how to extract the SOAP header information.

I'm using Spring 3, but haven't seen any methods/util classes there either that would shed some light on the issue. From what I've seen online, I can use getMessageContext() and cast to a SOAPMessageContext, but I see significant numbers of people failing at that level complaining about casting problems with no offered solution.

I have not yet tried it, so before I did, I was wondering if this was the preferred method, or if there is a better technique to use.

Thanks,

Eric


Solution

  • I guess that you can get the SoapMessageContext with a message handler. You can check the response here.

    Implementing a SoapMessageHandler like this should work:

    public class SoapHeadersHandler implements SOAPHandler<SOAPMessageContext>
    {
    
      @Override
      public boolean handleMessage(SOAPMessageContext soapMessageContext)
      {
        try
        {
          Object[] headers = soapMessageContext.getHeaders(...);
        }
        catch (SOAPException e)
        {
          // Handle exception
        }
    
        return true;
      }
    
    }