Search code examples
javaspringspring-bootsoapwsdl

Set Correct header for SOAP request


I have a generated class from a SOAP api.

When Sending out the request it sends in this format:

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

But what the api expects is:

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

My Login Class:

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

When i add namespace = "http://example.org/test_Service" to the @XmlRootElement for the Login class I get:

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

How can I set the correct namespace in the right format?

Thanks!


Solution

  • Eventually found a solution to my problem myself, but my implementation had to change.

    First I used SOAPPart and SOAPEnvelope to create my request. I could then explicitly set my namespace declarations. I folowed the implementation here

    Then my generated classes changed a bit

    Login Class

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