为SOAP请求设置正确的标头

时间:2017-12-07 10:13:26

标签: java spring spring-boot soap wsdl

我有一个来自SOAP api的生成类。

发送请求时,它会以这种格式发送:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<Login>
<username>Ayo.K</username>
<password>password</password>
</Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

但api期望的是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:adm="http://example.org/test_Service">
<soapenv:Header/>
<soapenv:Body>
<adm:Login>
<adm:username>Ayo.K</adm:username>
<adm:password>password</adm:password>
</adm:Login>
</soapenv:Body>
</soapenv:Envelope>

我的登录类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
        name = "",
        propOrder = {"username", "password"}
)
@XmlRootElement(
        name = "Login",
)
public class Login {
    protected String username;
    protected String password;

    public Login() {
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String value) {
        this.username = value;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String value) {
        this.password = value;
    }
}

当我为namespace = "http://example.org/test_Service"课程的@XmlRootElement添加Login时,我得到了:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns3:Login xmlns:ns3="http://example.org/test_Service">
<username>Ayo.K</username>
<password>password</password>
</ns3:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如何以正确的格式设置正确的命名空间?

谢谢!

1 个答案:

答案 0 :(得分:0)

最终我自己找到了解决问题的方法,但我的实施必须改变。

首先,我使用SOAPPartSOAPEnvelope来创建我的请求。然后我可以显式设置我的命名空间声明。我接受了实施here

然后我生成的类改变了一点

Login班级

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
        name = "",
        propOrder = {"username", "password"}
)
@XmlRootElement(
        name = "Login",
        namespace = "http://example.org/test_Service"
)
public class Login {
    protected String username;
    protected String password;

    public Login() {
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String value) {
        this.username = value;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String value) {
        this.password = value;
    }
}