我已经获得了我需要使用的 SOAP Web 服务的 wsdl。我已经使用 wsdl 在 netbeans 中创建 web 服务类。
SOAP 标头需要带有用户名和密码的 ServiceAuthHeader。
NetBeans 确实生成了一个 ServiceAuthHeader 类,但我不知道如何将它添加到使用生成的类发送的 SOAP 消息中。
我知道如何在较低级别执行此操作,即创建一个 SOAPMEssage,添加标头,连接到服务并发送它,但是我以前从未使用过 jws,其中的细节已经为您完成了,我正在努力找出我在任何文档或教程中添加它的位置。
生成的 ServiceAuthHeader 是这样的:
package com.theservice.webservice;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;
/**
* <p>Java class for ServiceAuthHeader complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="ServiceAuthHeader">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Username" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* <anyAttribute/>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ServiceAuthHeader", propOrder = {
"username",
"password"
})
public class ServiceAuthHeader {
@XmlElement(name = "Username")
protected String username;
@XmlElement(name = "Password")
protected String password;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
/**
* Gets the value of the username property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUsername() {
return username;
}
/**
* Sets the value of the username property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUsername(String value) {
this.username = value;
}
/**
* Gets the value of the password property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getPassword() {
return password;
}
/**
* Sets the value of the password property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setPassword(String value) {
this.password = value;
}
/**
* Gets a map that contains attributes that aren't bound to any typed property on this class.
*
* <p>
* the map is keyed by the name of the attribute and
* the value is the string value of the attribute.
*
* the map returned by this method is live, and you can add new attribute
* by updating the map directly. Because of this design, there's no setter.
*
*
* @return
* always non-null
*/
public Map<QName, String> getOtherAttributes() {
return otherAttributes;
}
}
所以我可以使用这个成功地调用服务:
private static PriceDetailRetunValue priceDetail(PriceDetailInputValue inputValue) {
com.theservice.webservice.WebService service = new com.theservice.webservice.WebService();
com.theservice.webservice.WebServiceSoap port = service.getWebServiceSoap12();
return port.priceDetail(inputValue);
}
我可以解析响应,这当然告诉我需要提供凭据。
那么我如何处理实际的 SOAP 标头消息以便能够添加 ServiceAuthHeader?我一直在查看创建的 WebService 的方法,并看到您可以获得请求上下文,并且我已经看到了如何将凭据添加到 http 请求标头,但我无法找到要添加到的任何地方SOAPME 消息。
任何帮助,将不胜感激。谢谢。