如何创建具有动态标头的JAX-WS Webservice Client

时间:2017-09-23 07:32:20

标签: java web-services soap proxy jax-ws

我必须为JAX-WS Web服务创建一个客户端,它希望填充标题。

Body元素我能够正确填充,但我不确定我是否能够正确填充标题。

我无法找到一种方法,可以在Web服务的加载代理中正确填充标题。

标头有一个Security标头,有3个元素genid,Proj_name,username。

正文有一个PN_SET_PO方法,它有3个元素数据,integration_id,Proj_name。

我可以正确设置正文,但我的问题是如何设置标题。如果我查看代理加载的类,我会看到一个安全头类,可用于设置安全头,但如何将其传递给请求。

在代理中有一个Object Factory类,它也有方法,但不知道如何使用它们。

我知道处理这个的一种方法是使用soap的处理程序类。

我的问题是来自代理的加载类确实有填充标题的方法我不知道如何使用它们。

我已将整个代码上传到谷歌驱动器

https://drive.google.com/file/d/0B5mLXgRFpKi8SjNsS1hHWk5BTUk/view?usp=sharing

此致 拉姆

...................主类代码.......................

package javawebcustom.proxy.types;

import com.sun.org.apache.xerces.internal.dom.ElementNSImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import javawebcustom.proxy.types.PayableProject;
import javawebcustom.proxy.types.PayableProjectSoap;
import javawebcustom.proxy.types.types.LoginResponse;
import javawebcustom.proxy.types.types.PNSETDATA;
import javawebcustom.proxy.types.types.PNSETDATAResponse;
import javawebcustom.proxy.soaphandler.HeaderHandler;
import javawebcustom.proxy.soaphandler.HeaderHandlerResolver;
import javawebcustom.proxy.valueobject.InternalVO_B5543PONE;
import javawebcustom.proxy.BusinessService;
import javawebcustom.proxy.IContext;
import javawebcustom.proxy.connection.IConnection;
import javawebcustom.proxy.util.E1MessageList;
import javawebcustom.proxy.util.SoftCodingRecordAccess;
import javawebcustom.proxy.exception.InvalidSoftCodingRecordException;
import javawebcustom.proxy.exception.ServicePropertyException;
import javawebcustom.proxy.util.ServicePropertyAccess;
import org.w3c.dom.Element;
import java.net.MalformedURLException;
import java.net.URL;

public class SendPOOrder extends BusinessService {

public static  callPO(IContext context, IConnection connection, XML) {

    URL wsdlLocation = null;
    String username = "yesu";
    String password = "";
    String proj_name = "TEST";
    String projip = "3XY";
    String genid = "";
    String url ="";

    int Flag = 0;




         genid = FAC11;

        int integrationId = 21;



     *  ============================ */  

     // I am setting the header here 

        SecurityHeader sh = new SecurityHeader();
        sh.setProjname(proj_name);
        hh.setgenid(genid);
        hh.setUserName(username);

         PurchaseOrder purchaseOrder = new PurchaseOrder(wsdlLocation);
        PurchaseOrderSoap purchaseOrderSoap = PurchaseOrder.getPurchaseOrderSoap();

        Map<String, Object> reqContext = ((BindingProvider) payablePurchaseSoap).getRequestContext();
        reqContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
        reqContext.put(BindingProvider.USERNAME_PROPERTY, username);
        reqContext.put(BindingProvider.PASSWORD_PROPERTY, password);

        PNSETPO.Data data = new PNSETPO.Data();

        try 
        {
            XMLDoc = "Here will be my XML";
            if (XMLDoc != null)
            {
                data.getContent().add(XMLDoc);

     // This is the body part

                PNSETPOResponse.PNSETPOResult response = 
                        purchaseOrderSoap.pnSETPO(data, datatype, integrationId, proj_name);

                                        }

   }



}

}

我的问题是如何填充标题。代理有两种方法,一种是我在代码中使用的安全头,另一种是Object Factory,它们都在从webservice加载的代理中

...安全标题代码................

package javawebcustom.proxy.types;

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 SecurityHeader complex type.
  *
  * <p>The following schema fragment specifies the expected content 
       contained 
        within this class.
  *
  * <pre>
  * &lt;complexType name="SecurityHeader">
  *   &lt;complexContent>
  *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
  *       &lt;sequence>
  *         &lt;element name="genid" type="
  {http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
  *         &lt;element name="proj_name" type="
  {http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
  *         &lt;element name="UserName" type="
  {http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
   *         &lt;element name="ImagePath" 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 = "SecurityHeader", propOrder = { "genid", "projname", 
      "userName", "imagePath" })
        public class SecurityHeader {

@XmlElement(name = "genid")
protected String genid;
@XmlElement(name = "proj_name")
protected String projname;
@XmlElement(name = "UserName")
protected String userName;
@XmlElement(name = "ImagePath")
protected String imagePath;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();

/**
 * Gets the value of the genid property.
 *
 * @return
 *     possible object is
 *     {@link String }
 *
 */
public String getgenid() {
    return genid;
}

/**
 * Sets the value of the genid property.
 *
 * @param value
 *     allowed object is
 *     {@link String }
 *
 */
public void setgenid(String value) {
    this.genid = value;
}

/**
 * Gets the value of the projname property.
 *
 * @return
 *     possible object is
 *     {@link String }
 *
 */
public String getProjName() {
    return projname;
}

/**
 * Sets the value of the projname property.
 *
 * @param value
 *     allowed object is
 *     {@link String }
 *
 */
public void setProjName(String value) {
    this.projname = value;
}

/**
 * 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 imagePath property.
 *
 * @return
 *     possible object is
 *     {@link String }
 *
 */
public String getImagePath() {
    return imagePath;
}

/**
 * Sets the value of the imagePath property.
 *
 * @param value
 *     allowed object is
 *     {@link String }
 *
 */
public void setImagePath(String value) {
    this.imagePath = 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;
}

       }

下面是对象工厂代码

.................对象工厂代码.........................

  package javawebcustom.proxy.types;

   import javax.xml.bind.JAXBElement;
   import javax.xml.bind.annotation.XmlElementDecl;
   import javax.xml.bind.annotation.XmlRegistry;
   import javax.xml.namespace.QName;


   /**
   * This object contains factory methods for each
    * Java content interface and Java element interface
    * generated in the javawebcustom.proxy.types package.
    * <p>An ObjectFactory allows you to programatically
      * construct new instances of the Java representation
      * for XML content. The Java representation of XML
      * content can consist of schema derived interfaces 
      * and classes representing the binding of schema
      * type definitions, element declarations and model
      * groups.  Factory methods for each of these are
      * provided in this class.
      *
       */
      @XmlRegistry
      public class ObjectFactory {

private final static QName _SecurityHeader_QNAME =
    new QName("http://www.purchaseorder.com/poservices", "SecurityHeader");

/**
 * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: javawebcustom.proxy.types
 *
 */
public ObjectFactory() {
}

/**
 * Create an instance of {@link PNGETPOResponse }
 *
 */
public PNGETPOResponse createPNGETPOResponse() {
    return new PNGETPOResponse();
}

/**
 * Create an instance of {@link PNPODATAResponse }
 *
 */
public PNPODATAResponse createPNPODATAResponse() {
    return new PNPODATAResponse();
}

/**
 * Create an instance of {@link PNGETPO }
 *
 */
public PNGETPO createPNGETPO() {
    return new PNGETPO();
}

/**
 * Create an instance of {@link PNPODATA }
 *
 */
public PNPODATA createPNPODATA() {
    return new PNPODATA();
}

/**
 * Create an instance of {@link LoginResponse }
 *
 */
public LoginResponse createLoginResponse() {
    return new LoginResponse();
}

/**
 * Create an instance of {@link Login }
 *
 */
public Login createLogin() {
    return new Login();
}

/**
 * Create an instance of {@link PNGETPOResponse.PNGETPOResult }
 *
 */
public PNGETPOResponse.PNGETPOResult createPNGETPOResponsePNGETPOResult() {
    return new PNGETPOResponse.PNGETPOResult();
}

/**
 * Create an instance of {@link PNPODATAResponse.PNPODATAResult }
 *
 */
public PNPODATAResponse.PNPODATAResult createPNPODATAResponsePNPODATAResult() {
    return new PNPODATAResponse.PNPODATAResult();
}

/**
 * Create an instance of {@link PNGETPO.Xmldata }
 *
 */
public PNGETPO.Xmldata createPNGETPOXmldata() {
    return new PNGETPO.Xmldata();
}

/**
 * Create an instance of {@link SecurityHeader }
 *
 */
public SecurityHeader createSecurityHeader() {
    return new SecurityHeader();
}

/**
 * Create an instance of {@link PNPODATA.Data }
 *
 */
public PNPODATA.Data createPNPODATAData() {
    return new PNPODATA.Data();
}

/**
 * Create an instance of {@link LoginResponse.LoginResult }
 *
 */
public LoginResponse.LoginResult createLoginResponseLoginResult() {
    return new LoginResponse.LoginResult();
}

/**
 * Create an instance of {@link JAXBElement }{@code <}{@link SecurityHeader }{@code >}}
 *
 */
@XmlElementDecl(namespace = "http://www.purchaseorder.com/poservices", name = "SecurityHeader")
public JAXBElement<SecurityHeader> createSecurityHeader(SecurityHeader value) {
    return new JAXBElement<SecurityHeader>(_SecurityHeader_QNAME, SecurityHeader.class, null, value);
}

   }

0 个答案:

没有答案