首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义Spring集成Web服务SOAP信封/报头

自定义Spring集成Web服务SOAP信封/报头
EN

Stack Overflow用户
提问于 2016-09-08 20:08:51
回答 2查看 1.2K关注 0票数 1

我试图使用发送SOAP请求,如

代码语言:javascript
复制
<int:chain input-channel="wsOutChannel" output-channel="stdoutChannel">
    <int-ws:header-enricher>
        <int-ws:soap-action value="..."/>
    </int-ws:header-enricher>
    <int-ws:outbound-gateway
            uri="..."/>
</int:chain>

但是您只能添加SOAP,添加了信封、标头和正文标记,如

代码语言:javascript
复制
<SOAP-ENV:Envelope>
    <SOAP-ENV:Header>
        <SOAP-ENV:Body>
            ...
        </SOAP-ENV:Body>
    <SOAP-ENV:Header>
</SOAP-ENV:Envelope>

我需要定制带有特定属性的信封和标头标记,例如:

代码语言:javascript
复制
<soapenv:Envelope attribute1="value1" attribute2="value2">

和子元素,例如:

代码语言:javascript
复制
<soapenv:Header>
    <child>...<child>
<soapenv:Header>

这在中是可能的吗,还是我不应该使用int-ws:outbound-gateway并采取不同的方法呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-08 20:24:21

您可以添加一个ClientInterceptor (通过interceptor属性),它允许您在发出请求之前修改它。

编辑

@Artem的建议更简单,但拦截器也允许您访问响应;但无论哪种方式,代码都是类似的。

对于拦截器:

代码语言:javascript
复制
public class MyInterceptor extends ClientInterceptorAdapter {

    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        SoapMessage request = (SoapMessage) messageContext.getRequest();
        SoapEnvelope envelope = request.getEnvelope();
        envelope.addAttribute(new QName("foo"), "bar");
        SoapHeader header = envelope.getHeader();
        header.addHeaderElement(new QName("http://fiz/buz", "baz"));
        return super.handleRequest(messageContext);
    }

}

对于回调版本:

代码语言:javascript
复制
@Override
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
    SoapEnvelope envelope = ((SoapMessage) message).getEnvelope();
    envelope.addAttribute(new QName("foo"), "bar");
    SoapHeader header = envelope.getHeader();
    header.addHeaderElement(new QName("http://fiz/buz", "baz"));
}
票数 1
EN

Stack Overflow用户

发布于 2016-09-08 20:25:32

我想你可以注射WebServiceMessageCallback

代码语言:javascript
复制
<xsd:attribute name="request-callback" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation>
Reference to a Spring Web Services WebServiceMessageCallback. This enables changing
the Web Service request message after the payload has been written to it but prior
to invocation of the actual Web Service.
                </xsd:documentation>
                <xsd:appinfo>
                    <tool:annotation kind="ref">
                        <tool:expected-type type="org.springframework.ws.client.core.WebServiceMessageCallback"/>
                    </tool:annotation>
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:attribute>

并将消息传递给SoapMessage,并使用其getEnvelope()自定义所需的方式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39399377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档