java - 如何在Spring Integration中设置soap header ?

标签 java spring soap spring-integration

我想使用 Spring Integration 发送一条 SOAP 消息。我使用Java配置。 我已经尝试过下面的拦截器,但是 spring 集成将尖括号 (<>) 转换为 html 转义字符。

import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.SoapMessage;

public class MyAuthInterceptor implements ClientInterceptor {
    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
        SoapHeader sh = soapMessage.getSoapHeader();
        QName name = new QName("http://...", "myAuth", "aut");
        sh.addHeaderElement(name).setText("<username>TestUser</username>" + "<password>TestPass</password>");
        return true;
    }

这是生成的 SOAP 头:

<SOAP-ENV:Header>
    <aut:myAuth xmlns:aut="http://.../">&lt;username&gt;TestUser&lt;/username&gt;&lt;password&gt;TestPass&lt;/password&gt;</aut:myAuth>
</SOAP-ENV:Header>

这是我的配置:

@Configuration
@EnableIntegration
public class SpringIntegrationConfiguration {

    @Bean
    public PublishSubscribeChannel inputChannel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    public ClientInterceptor myAuthInterceptor() {
        return new MyAuthInterceptor();
    }

    @Bean
    @ServiceActivator(inputChannel = "inputChannel")
    public SimpleWebServiceOutboundGateway myOutput(ClientInterceptor mekAuthInterceptor) {
        SimpleWebServiceOutboundGateway simpleWebServiceOutboundGateway = new SimpleWebServiceOutboundGateway("http://...");
        simpleWebServiceOutboundGateway.setInterceptors(myAuthInterceptor);
        return simpleWebServiceOutboundGateway;
    }
}

如何在不脱离尖括号的情况下设置 SOAP 头?

最佳答案

您必须使用 addChildElement 来构建它,而不是将其设置为文本。

关于java - 如何在Spring Integration中设置soap header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35946417/

相关文章:

java - 如何创建动态记录行以使用列表创建 xls 文件

java - 如何通过非硬编码 key 使用 Redshift COPY

java - 如何在 Netbeans 中配置 spring 配置文件?

php - 在 cent0s 6.7 中安装 php-soap 未显示在 php-info 中

android - 在 KSOAP2 请求中对 Android 中的 .NET web 服务进行身份验证

delphi - Delphi 的断开连接的记录集/数据集

java - 获取文件夹和子文件夹 ID Google Drive

java - 来自 junit 测试的 "Forked Java VM exited abnormally"错误

spring - 在 Camunda 条件下使用 Spring Bean

java - 如何解决BeanCreationException?