Java 仅JAXB封送/解封送根节点

Java 仅JAXB封送/解封送根节点,java,jaxb,ejb-3.0,Java,Jaxb,Ejb 3.0,我是JAXB的新手。我所要做的就是,给定一个现有的xml字符串,将其封送,并在ejb中返回它。问题是以下代码似乎只适用于根节点,而不适用于子节点。我希望它能对整个事情起作用。如果有帮助的话,我没有修改任何通过输入方案生成的代码。任何提示都会有帮助的,谢谢 import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.Stateless; import javax.jws.WebService;

我是JAXB的新手。我所要做的就是,给定一个现有的xml字符串,将其封送,并在ejb中返回它。问题是以下代码似乎只适用于根节点,而不适用于子节点。我希望它能对整个事情起作用。如果有帮助的话,我没有修改任何通过输入方案生成的代码。任何提示都会有帮助的,谢谢

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import org.netbeans.j2ee.wsdl.test.newwsdl.NewWSDLPortType;
import org.netbeans.xml.schema.newxmlschema.*;

@WebService(serviceName = "newWSDLService", portName = "newWSDLPort", endpointInterface = "org.netbeans.j2ee.wsdl.test.newwsdl.NewWSDLPortType", targetNamespace = "http://j2ee.netbeans.org/wsdl/test/newWSDL", wsdlLocation = "META-INF/wsdl/NewWebServiceFromWSDL/newWSDLWrapper.wsdl")
@Stateless
public class NewWebServiceFromWSDL implements NewWSDLPortType {

    public Root newWSDLOperation() {
        String xml = 
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
        +"<ns0:root  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
        +"   xmlns:ns0='http://xml.netbeans.org/schema/newXmlSchema'"
        +"   xsi:schemaLocation='http://xml.netbeans.org/schema/newXmlSchema newXmlSchema.xsd'>"
        +"    <ns0:node>"
        +"        <ns0:name>asdf</ns0:name>"
        +"        <ns0:value>asdf</ns0:value>"
        +"        <ns0:date>2009-01-01</ns0:date>"
        +"    </ns0:node>"
        +"    <ns0:node>"
        +"        <ns0:name>asdf</ns0:name>"
        +"        <ns0:value>asdf</ns0:value>"
        +"        <ns0:date>2009-01-01</ns0:date>"
        +"    </ns0:node>"
        +"    <ns0:node>"
        +"        <ns0:name>asdf</ns0:name>"
        +"        <ns0:value>asdf</ns0:value>"
        +"        <ns0:date>2009-01-01</ns0:date>"
        +"    </ns0:node>"
        +"</ns0:root>";
        Root root = null;
        try {
            root = jaxbUnmarshalFromString(xml);
        } catch (Throwable ex) {
            Logger.getLogger(NewWebServiceFromWSDL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return root;
    }

    private Root jaxbUnmarshalFromString(String str) throws javax.xml.bind.JAXBException {
        Root ret = null;
        javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(Root.class.getPackage().getName());
        javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        ret = (Root) ((JAXBElement)unmarshaller.unmarshal(new java.io.StringReader(str))).getValue();

        return ret;
    }

}
* * * *列表中允许以下类型的对象 *{@link Node} * * */ 公共列表获取节点{ 如果节点==null{ node=newarraylist; } 返回此.node; } } 包org.netbeans.xml.schema.newxmlschema; 导入javax.xml.bind.annotation.XmlAccessType; 导入javax.xml.bind.annotation.XmlAccessorType; 导入javax.xml.bind.annotation.xmlement; 导入javax.xml.bind.annotation.XmlSchemaType; 导入javax.xml.bind.annotation.XmlType; 导入javax.xml.datatype.XMLGregorianCalendar; /** *用于节点复杂类型的Java类。 * *以下架构片段指定此类中包含的预期内容。 * * * * */ @XmlAccessorTypeXmlAccessType.FIELD @XmlTypename=节点,比例器={ 名称 价值 日期 } 公共类节点{ @XmlElementrequired=true 受保护的字符串名称; @XmlElementrequired=true 保护字符串值; @XmlElementrequired=true @XmlSchemaTypename=日期 受保护的XMLGregorianCalendar日期; /** *获取name属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getName{ 返回名称; } /** *设置name属性的值。 * *@param值 *允许的对象是 *{@link String} * */ public void setNameString值{ this.name=值; } /** *获取value属性的值。 * *@返回 *可能的对象是 *{@link String} * */ 公共字符串getValue{ 返回值; } /** *设置值属性的值。 * *@param值 *允许的对象是 *{@link String} * */ public void setValueString值{ 这个值=值; } /** *获取date属性的值。 * *@返回 *可能的对象是 *{@link XmlGregorianalendar} * */ 公共XMLGregorianCalendar getDate{ 返回日期; } /** *设置日期属性的值。 * *@param值 *允许的对象是 *{@link XmlGregorianalendar} * */ 公共无效setDateXMLGregorianCalendar值{ this.date=值; } } 和WSDL:

try {
     root = jaxbUnmarshalFromString(xml);
} catch (Throwable ex) {
     // !!! Exception caught here and not rethrown
     Logger.getLogger(NewWebServiceFromWSDL.class.getName()).log(Level.SEVERE, null, ex);
}
return root; // !!!root is null here 
以下soap调用:

返回:


您在编组后吞下异常


我的猜测是,当您返回root时,它是空的。在记录异常时检查日志

JAXB解组器返回空的根注释时,我遇到了一个问题。事实证明,在使用PowerMockito运行JUnit测试时,我只得到空的根节点。我想知道在幕后是否有什么事情阻止了真正的解组程序的运行。

它似乎在root上也不起作用。它只返回一个空的消息名part1,在编组后您将吞下异常。我的猜测是,当您返回root时,它是空的。检查日志。日志中没有任何内容。我在调试器中浏览了它,没有抛出异常。root在返回时不是null,但它的节点对象是。嗯,好的。奇怪的您可以发布生成的类吗?
package org.netbeans.xml.schema.newxmlschema;

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

@XmlRegistry
public class ObjectFactory {

    private final static QName _Root_QNAME = new QName("http://xml.netbeans.org/schema/newXmlSchema", "root");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.netbeans.xml.schema.newxmlschema
     * 
     */
    public ObjectFactory() {
    }

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

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

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Root }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "http://xml.netbeans.org/schema/newXmlSchema", name = "root")
    public JAXBElement<Root> createRoot(Root value) {
        return new JAXBElement<Root>(_Root_QNAME, Root.class, null, value);
    }

}


package org.netbeans.xml.schema.newxmlschema;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**    
 * <p>Java class for root complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="root">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="node" type="{http://xml.netbeans.org/schema/newXmlSchema}node" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "root", propOrder = {
    "node"
})
public class Root {

    protected List<Node> node;

    /**
     * Gets the value of the node property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the node property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getNode().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Node }
     * 
     * 
     */
    public List<Node> getNode() {
        if (node == null) {
            node = new ArrayList<Node>();
        }
        return this.node;
    }

}


package org.netbeans.xml.schema.newxmlschema;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;


/**
 * <p>Java class for node complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="node">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="value" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="date" type="{http://www.w3.org/2001/XMLSchema}date"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "node", propOrder = {
    "name",
    "value",
    "date"
})
public class Node {

    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected String value;
    @XmlElement(required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar date;

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

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

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

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

    /**
     * Gets the value of the date property.
     * 
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getDate() {
        return date;
    }

    /**
     * Sets the value of the date property.
     * 
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setDate(XMLGregorianCalendar value) {
        this.date = value;
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="newWSDL" targetNamespace="http://j2ee.netbeans.org/wsdl/test/newWSDL"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://j2ee.netbeans.org/wsdl/test/newWSDL" xmlns:ns="http://xml.netbeans.org/schema/newXmlSchema" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype">
    <types>
        <xsd:schema targetNamespace="http://j2ee.netbeans.org/wsdl/test/newWSDL">
            <xsd:import namespace="http://xml.netbeans.org/schema/newXmlSchema" schemaLocation="newXmlSchema.xsd"/>
        </xsd:schema>
    </types>
    <message name="newWSDLOperationRequest"/>
    <message name="newWSDLOperationResponse">
        <part name="part1" type="ns:root"/>
    </message>
    <portType name="newWSDLPortType">
        <operation name="newWSDLOperation">
            <input name="input1" message="tns:newWSDLOperationRequest"/>
            <output name="output1" message="tns:newWSDLOperationResponse"/>
        </operation>
    </portType>
    <plnk:partnerLinkType name="newWSDL">
        <plnk:role name="newWSDLPortTypeRole" portType="tns:newWSDLPortType"/>
    </plnk:partnerLinkType>
</definitions>
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header/>
    <S:Body>
        <ns2:newWSDLOperation xmlns:ns2="http://j2ee.netbeans.org/wsdl/test/newWSDL" xmlns:ns3="http://xml.netbeans.org/schema/newXmlSchema"/>
    </S:Body>
</S:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns3:newWSDLOperationResponse xmlns:ns2="http://xml.netbeans.org/schema/newXmlSchema" xmlns:ns3="http://j2ee.netbeans.org/wsdl/test/newWSDL">
            <part1/>
        </ns3:newWSDLOperationResponse>
    </S:Body>
</S:Envelope>
try {
     root = jaxbUnmarshalFromString(xml);
} catch (Throwable ex) {
     // !!! Exception caught here and not rethrown
     Logger.getLogger(NewWebServiceFromWSDL.class.getName()).log(Level.SEVERE, null, ex);
}
return root; // !!!root is null here