如何在Soap Request Java中设置标头

时间:2017-03-20 06:23:00

标签: java web-services soap wsdl jax-ws

我在形成SOAP请求时遇到了问题。

在该请求中,我应该在标题部分添加用户名,密码和其他一些信息而不是有效负载的部分。

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

和InputUploadCustomerDocument Java文件在下面,该文件没有用户名,密码和其他字段,我需要在进行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;
}

}

这是我需要调用的功能

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

有人可以帮我设置这些标题吗?

1 个答案:

答案 0 :(得分:1)

在发出请求之前,您可以使用下面的行来添加标头,因为您正在使用JAX-WS:

    $.fn.fileUploader = function (filesToUpload, sectionIdentifier) {
    var fileIdCounter = 0;

    this.closest("#sell-images").change(function (evt) {
        var output = [];
        for (var i = 0; i < evt.target.files.length; i++) {
            fileIdCounter++;
            var file = evt.target.files[i];
            var fileId = sectionIdentifier + fileIdCounter;

            filesToUpload.push({
                id: fileId,
                file: file
            });
        };

        for(var z = 0; z < filesToUpload.length; z++)
        {
            evt.target.files[z] = filesToUpload[z]['file'];
        }
        console.log(evt.target.files)
    });

    return this;
};

您可以参考很多教程。转到谷歌并搜索使用SOAP Web服务。以下是您可以参考的一个这样的教程:

enter image description here

这是您可以使用的另一个好例子: http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client/

希望这有帮助。