'How to set headers in Soap Request Java

I am facing issue in forming a SOAP request.

In that request i am supposed to add username , password and some other info in header part not the part of payload.

Below the Entry in wsdl

<wsdl:message name="InputUploadCustomerDocument_Headers">
<wsdl:part name="DocumentType" element="tns:DocumentType"/>
<wsdl:part name="FileName" element="tns:FileName"/>
<wsdl:part name="Password" element="tns:Password"/>
<wsdl:part name="PinNo" element="tns:PinNo"/>
<wsdl:part name="UserName" element="tns:UserName"/>
</wsdl:message>
<wsdl:message name="ReturnUploadCustomerDocument">
<wsdl:part name="parameters" element="tns:ReturnUploadCustomerDocument"/>
</wsdl:message>


<wsdl:operation name="UploadCustomerDocument">
<soap:operation soapAction="http://tempuri.org/ISend/UploadCustomerDocument" style="document"/>
<wsdl:input name="InputUploadCustomerDocument">
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="DocumentType" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="FileName" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="Password" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="PinNo" use="literal"/>
<soap:header message="tns:InputUploadCustomerDocument_Headers" part="UserName" use="literal"/>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="ReturnUploadCustomerDocument">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>

And the InputUploadCustomerDocument Java File Below, the file don't have username, password and other fields, and i need to set these parameters before making reuqest

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"fileData"
})
@XmlRootElement(name = "InputUploadCustomerDocument")
public class InputUploadCustomerDocument {

@XmlElement(name = "FileData", required = true)
protected byte[] fileData;

/**
 * Gets the value of the fileData property.
 * 
 * @return
 *     possible object is
 *     byte[]
 */
public byte[] getFileData() {
    return fileData;
}

/**
 * Sets the value of the fileData property.
 * 
 * @param value
 *     allowed object is
 *     byte[]
 */
public void setFileData(byte[] value) {
    this.fileData = value;
}

}

Here is the function i need to call

@WebMethod(operationName = "UploadCustomerDocument", action = "http://tempuri.org/ISend/UploadCustomerDocument")
@WebResult(name = "ReturnUploadCustomerDocument", targetNamespace = "http://tempuri.org/", partName = "parameters")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public ReturnUploadCustomerDocument uploadCustomerDocument(
    @WebParam(name = "InputUploadCustomerDocument", targetNamespace = "http://tempuri.org/", partName = "parameters")
    InputUploadCustomerDocument parameters);

Can someone help how i can set these headers?



Solution 1:[1]

You can use this below line to add header before making a request, since you're using JAX-WS:

SOAPHeader header = envelope.addHeader();

There are plenty of tutorials that you could refer to. Go to google and search for consuming SOAP Web Service. Here is one such tutorial you can refer to:

http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client/

Here is another good example you could use: https://soa2world.blogspot.com/2009/05/direct-web-service-client-using-java.html

Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1