Web Services 如何在SOAP标头中检索From/Address值(在Java/SpringBoot中)

wztqucjr  于 2022-11-15  发布在  Java
关注(0)|答案(1)|浏览(141)

我尝试使用此教程创建Sping Boot SOAP应用程序:https://www.baeldung.com/spring-boot-soap-web-service到目前为止,一切都运转得很好。

任务:我想访问SOAP标头。

这是我目前所做的:

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

我可以用MessageContext来扩展它:

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

这个MessageContext加上一点util方法fooBar应该用来访问SOAP头:

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

到目前为止一切都很好。下面是我的SOAP消息:

<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>

我想获取<From><Address>中的地址:http://from-endpoint..但是现在它开始变得很难用了..

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")) {
 [...]

这和预期的一样,但是这个SoapHeaderElementorg.springframework.ws.soap.SoapHeaderElement类型,它没有提供访问其他子元素的选项。
"我做错了什么"
... ... ...
解决方案已找到-见评论

pdkcd3nj

pdkcd3nj1#

多亏了M. Deinum的帮助,我找到了一个解决方案:

@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();
    }

相关问题