简体   繁体   中英

How to retrieve From/Address value in SOAP header (in Java/SpringBoot)

I'm trying to create a Spring Boot SOAP application using this tutorial: https://www.baeldung.com/spring-boot-soap-web-service Everything works like a charm so far.

Task: I'd like to access the SOAP header.

This is what I've done so far:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
    [..]
}

I can just extend this with MessageContext :

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request, MessageContext messageContext) {
    [..]
}

This MessageContext plus a little util method fooBar is supposed to access the SOAP header:

protected static String fooBar(final MessageContext messageContext) {
    SoapHeader soapHeader = ((SoapMessage) messageContext.getRequest()).getSoapHeader();
    [...]
}

So far so good. This is what my SOAP message looks like:

<soap:Envelope
        xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
        [...]
        xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <soap:Header>
        [...]
        <wsa:From>
            <wsa:Address>http://from-endpoint</wsa:Address>
        </wsa:From>
        [...]
    </soap:Header>
    <soap:Body>
        [...]
    </soap:Body>
</soap:Envelope>

I would like to get the address in <From><Address> : http://from-endpoint.. but now it starts to get awkward..

protected static String fooBar(final MessageContext messageContext) {
    SoapHeader soapHeader = ((SoapMessage) messageContext.getRequest()).getSoapHeader();
    Iterator<SoapHeaderElement> soapHeaderElementIterator = soapHeader.examineAllHeaderElements();
    while (soapHeaderElementIterator.hasNext()) {
        SoapHeaderElement soapHeaderElement = soapHeaderElementIterator.next();
        if (soapHeaderElement.getName().getLocalPart().equals("From")) {
 [...]

This works as expected, but this SoapHeaderElement is type org.springframework.ws.soap.SoapHeaderElement which gives me no options to access further child elements.

What am I doing wrong here?

... ... ...

✅ SOLUTION has been found - see comments

Thanks to help of M. Deinum I've found a solution:

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request, MessageContext messageContext) {
        Addressing10 addressing10 = new Addressing10();
        MessageAddressingProperties messageAddressingProperties = addressing10.getMessageAddressingProperties((SoapMessage) messageContext.getRequest());
        URI address = messageAddressingProperties.getFrom().getAddress();
    }
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM