java - 使用 NetBeans 生成的 WebService 类将 ServiceAuthHeader 添加到 SOAP 消息

标签 java soap netbeans-7

我已经获得了我需要使用的 SOAP Web 服务的 wsdl。我已经使用 wsdl 在 netbeans 中创建 Web 服务类。

SOAP header 需要带有用户名和密码的 ServiceAuthHeader。

NetBeans 确实生成了一个 ServiceAuthHeader 类,但我不知道如何将其添加到使用生成的类发送的 SOAP 消息中。

我知道如何在较低级别执行此操作,即创建 SOAPMEssage、添加 header 、连接到服务并发送它,但我以前从未使用过 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 header 消息的句柄以便能够添加 ServiceAuthHeader?我一直在查看创建的 WebService 的方法,发现您可以获取请求上下文,并且我已经了解了如何将凭据添加到 http 请求 header ,但我还没有找到任何地方可以添加到SOAPMEssage。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

我在这里找到了答案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;
}
}

它需要一些技巧,但它可以完成任务。

关于java - 使用 NetBeans 生成的 WebService 类将 ServiceAuthHeader 添加到 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12492499/

相关文章:

java - 在java中拆分解析文本文件

java.security.Principal - 在 HttpServletRequest 和 Spring Security 中创建

java - 如何访问soap头信息?

java - 枚举静态数组和注解值混淆

tomcat - 在Tomcat中用 "manager-script"角色设置正确的用户名和密码

java - 检查文本字段和组合框中的条目以启用按钮 Java 桌面应用程序

java - JSF 2.0 使用 Eclipse?

java - 编译错误 - 在 Redhat "Openshift"应用程序中找不到包 com.google.gson

java - 从 WSDL URL 生成 WSDL 文件

java - 如何向 Java SOAP 客户端提供证书?