如何在Spring Integration中设置soap标头?

时间:2016-03-11 17:32:23

标签: java spring soap spring-integration

我想用Spring Integration发送一条肥皂消息。我使用Java Config。 我已尝试过flolowing拦截器,但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标头?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您正在将标记设置为文本,因为它被添加到xml中,因此会转义字符串。那些需要设置为元素

http://docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPHeaderElement.html

检查上述文档中的方法并正确使用它。如果您需要更多帮助,请发表评论。

答案 2 :(得分:0)

@ gary-russell-从逻辑的角度来看,您的回答是合理的,但是org.springframework.ws.soap。中没有“ addChildElement”方法。我在javax.xml.soap中找到了它们。

所以我的结果如下:

package main

import (
    "context"
    "fmt"
    "reflect"
)

type Fn func(context.Context)

type testStruct struct{}

func (*testStruct) DoSomething(context.Context) {}
func (*testStruct) DoSomethingElse([]byte)      {}

func main() {
    structType := reflect.TypeOf(&testStruct{})
    rctx := reflect.TypeOf(new(context.Context)).Elem()
    for i := 0; i < structType.NumMethod(); i++ {
        fmt.Println("======================")
        method := structType.Method(i)
        fmt.Println(method.Name)
        fmt.Println(method.Type.String())

        if method.Type.NumIn() != 2 {
            fmt.Println("wrong number of inputs, expected 1")
            continue
        }

        if method.Type.In(1) != rctx {
            fmt.Println("input of wrong type, expected context.Context")
            continue
        }

        if method.Type.NumOut() != 0 {
            fmt.Println("wrong number of outputs, expected 0")
            continue
        }

        fmt.Printf("%v is a function of correct type\n", method.Name)
    }
}
相关问题