0

我已经获得了我需要使用的 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>
 * &lt;complexType name="ServiceAuthHeader">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Username" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *       &lt;anyAttribute/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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 消息。

任何帮助,将不胜感激。谢谢。

4

1 回答 1

1

我在这里找到了答案http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client

您需要创建一个处理程序,然后告诉您的服务使用它。

所以我原来的方法只是添加了几行

private static PriceDetailRetunValue priceDetail(PriceDetailInputValue inputValue) {
        com.theservice.webservice.WebService service = new com.theservice.webservice.WebService();

    HeaderHandlerResolver handlerResolver = new HeaderHandlerResolver();
    service.setHandlerResolver(handlerResolver);

    com.theservice.webservice.WebServiceSoap port = service.getWebServiceSoap12();
    return port.priceDetail(inputValue);
}

HandlerResolver 和 Handler 看起来像这样......

package com.la.feed.xml.theservice;

import java.util.*;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.ws.handler.*;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {

@Override
public boolean handleMessage(SOAPMessageContext smc) {

    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {

        try {

            SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
            SOAPHeader header = envelope.addHeader();

            SOAPFactory soapFactory = SOAPFactory.newInstance();
            Name headerName = soapFactory.createName("ServiceAuthHeader", "", "http://www.interhome.com/webservice");
            SOAPHeaderElement headerElement = header.addHeaderElement(headerName);

            Name username = soapFactory.createName("Username");
            SOAPElement usernameElement = headerElement.addChildElement(username);
            usernameElement.addTextNode("GB1009688");

            Name password = soapFactory.createName("Password");
            SOAPElement passwordElement = headerElement.addChildElement(password);
            passwordElement.addTextNode("verbier");


        } catch (Exception e) {
        }

    }
    return outboundProperty;

}

@Override
public Set getHeaders() {
    return null;
}

@Override
public boolean handleFault(SOAPMessageContext context) {
    return true;
}

@Override
public void close(MessageContext context) {
}
}


package com.la.feed.xml.theservice;

import java.util.*;
import javax.xml.ws.handler.*;

public class HeaderHandlerResolver implements HandlerResolver {

@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
    List<Handler> handlerChain = new ArrayList<Handler>();

    HeaderHandler hh = new HeaderHandler();

    handlerChain.add(hh);

    return handlerChain;
}
}

它需要一点技巧,但它确实可以完成工作。

于 2012-09-24T08:32:07.323 回答