【问题标题】:How to set headers in Soap Request Java如何在 Soap 请求 Java 中设置标头
【发布时间】:2017-03-20 06:23:00
【问题描述】:

我在形成 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 File 下面,该文件没有用户名、密码等字段,需要在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】:

    由于您使用的是 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

    希望这会有所帮助。

    【讨论】:

    • 通过链接回答不好。两个链接都挂了。最好在这里复制一个 sn-p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2012-03-28
    相关资源
    最近更新 更多