如何在 Soap Request Java 中设置标头

问题描述 投票:0回答:1

我在形成 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);

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

java web-services soap wsdl jax-ws
1个回答
0
投票

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

SOAPHeader header = envelope.addHeader();

有很多教程可以参考。转到 google 并搜索消费 SOAP Web 服务。这是一个这样的教程,您可以参考:

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

这是您可以使用的另一个很好的例子: https://soa2world.blogspot.com/2009/05/direct-web-service-client-using-java.html

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.