2009-09-25 8 views
0

저는 JAXB를 처음 사용합니다. 원하는 것은 기존의 XML 문자열이 마샬링되고 ejb로 반환된다는 것입니다. 문제는 다음 코드는 자식 노드가 아니라 루트 노드에서만 작동하는 것입니다. 나는 그것이 모든 것을 위해 일하고 싶습니다. 그것이 도움이된다면, 나는 계획에서 먹이로 생성 된 코드를 변경하지 않았다. 모든 포인터 도움이 될 것입니다, 감사.JAXB 만 마샬링/언 마샬 루트 노드

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; 
    } 

} 

을 그리고 WSDL :

<?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> 

다음 SOA 체계에서 생성 된

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://xml.netbeans.org/schema/newXmlSchema" 
    xmlns:tns="http://xml.netbeans.org/schema/newXmlSchema" 
    elementFormDefault="qualified"> 
    <xsd:complexType name="root"> 
     <xsd:sequence> 
      <xsd:element name="node" type="tns:node" maxOccurs="unbounded" minOccurs="0"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:complexType name="node"> 
     <xsd:sequence> 
      <xsd:element name="name" type="xsd:string"/> 
      <xsd:element name="value" type="xsd:string"/> 
      <xsd:element name="date" type="xsd:date"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:element name="root" type="tns:root"/> 
</xsd:schema> 

클래스 : 여기

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; 
    } 

} 

는 계획이다 P 전화 :

<?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> 
+0

루트에서 작동하지 않는 것으로 보입니다. 비어있는 메시지 이름 "part1" –

+0

을 반환하면 마샬링 후에 예외가 삼킨다. 내 추측은 그 루트가 돌아올 때 루트가 null이라는 것입니다. 로그를 확인하십시오. –

답변

0

당신은 마샬링 후 예외를 삼키는.

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 

반환 할 때 루트가 null 인 것 같습니다. 예외가 기록 될 때 로그를 확인하십시오.

+0

로그에 아무것도 없습니다. 나는 그것을 디버거에서 처리했고 아무런 예외도 발생하지 않았다. 루트는 반환시 null이 아니지만 노드 객체입니다. –

+0

흠, 좋아. 이상한. 생성 된 클래스를 게시 할 수 있습니까? –

0

방금 ​​빈 루트 노트를 반환하는 JAXB unmarshaller에 문제가있었습니다. PowerMockito로 JUnit 테스트를 실행할 때 빈 루트 노드 만 얻습니다. "진짜"언 마샬 러가 실행되는 것을 막고있는 무언가가 무언가 일어나고 있는지 궁금합니다.