I have a web service response requirement where I need to send a response in formatted xml. I can't seem to get the JAXBElement requirements to wrap correctly. Any help on how to build and call these packages would be most appreciated.
ReturnMessage.java
package org.datacontract.schemas._2004._07.gn_ext_ws_contracts_datamember;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for ReturnMessage complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="ReturnMessage">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="ReturnCode" type="{http://www.w3.org/2001/XMLSchema}int"/>
* <element name="ReturnText" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="ReturnMessageDetails" type="{http://schemas.datacontract.org/2004/07/GN.Ext.WS.Contracts.DataMember.Common}ArrayOfReturnMessageDetail" minOccurs="1"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ReturnMessage", propOrder = {
"returnCode",
"returnText",
"returnMessageDetails"
})
public class ReturnMessage {
@XmlElement(name = "ReturnCode")
protected int returnCode;
@XmlElement(name = "ReturnText", required = true, nillable = true)
protected String returnText;
@XmlElementRef(name = "ReturnMessageDetails", namespace = "http://schemas.datacontract.org/2004/07/GN.Ext.WS.Contracts.DataMember.Common", type = JAXBElement.class)
protected JAXBElement<ArrayOfReturnMessageDetail> returnMessageDetails;
/**
* Gets the value of the returnCode property.
*
*/
public int getReturnCode() {
return returnCode;
}
/**
* Sets the value of the returnCode property.
*
*/
public void setReturnCode(int value) {
this.returnCode = value;
}
/**
* Gets the value of the returnText property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getReturnText() {
return returnText;
}
/**
* Sets the value of the returnText property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setReturnText(String value) {
this.returnText = value;
}
/**
* Gets the value of the returnMessageDetails property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link ArrayOfReturnMessageDetail }{@code >}
*
*/
public JAXBElement<ArrayOfReturnMessageDetail> getReturnMessageDetails() {
return returnMessageDetails;
}
/**
* Sets the value of the returnMessageDetails property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link ArrayOfReturnMessageDetail }{@code >}
*
*/
public void setReturnMessageDetails(JAXBElement<ArrayOfReturnMessageDetail> value) {
this.returnMessageDetails = ((JAXBElement<ArrayOfReturnMessageDetail> ) value);
}
}
ReturnMessageDetail.java
package org.datacontract.schemas._2004._07.gn_ext_ws_contracts_datamember;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for ReturnMessageDetail complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="ReturnMessageDetail">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="MessageKey" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="MessageType" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="MessageText" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ReturnMessageDetail", propOrder = {
"messageKey",
"messageType",
"messageText"
})
public class ReturnMessageDetail {
@XmlElementRef(name = "MessageKey", namespace = "http://schemas.datacontract.org/2004/07/GN.Ext.WS.Contracts.DataMember.Common", type = JAXBElement.class)
protected JAXBElement<String> messageKey;
@XmlElementRef(name = "MessageType", namespace = "http://schemas.datacontract.org/2004/07/GN.Ext.WS.Contracts.DataMember.Common", type = JAXBElement.class)
protected JAXBElement<String> messageType;
@XmlElementRef(name = "MessageText", namespace = "http://schemas.datacontract.org/2004/07/GN.Ext.WS.Contracts.DataMember.Common", type = JAXBElement.class)
protected JAXBElement<String> messageText;
/**
* Gets the value of the messageKey property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getMessageKey() {
return messageKey;
}
/**
* Sets the value of the messageKey property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setMessageKey(JAXBElement<String> value) {
this.messageKey = ((JAXBElement<String> ) value);
}
/**
* Gets the value of the messageType property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getMessageType() {
return messageType;
}
/**
* Sets the value of the messageType property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setMessageType(JAXBElement<String> value) {
this.messageType = ((JAXBElement<String> ) value);
}
/**
* Gets the value of the messageText property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getMessageText() {
return messageText;
}
/**
* Sets the value of the messageText property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setMessageText(JAXBElement<String> value) {
this.messageText = ((JAXBElement<String> ) value);
}
}
ArrayOfReturnMessageDetail.java
package org.datacontract.schemas._2004._07.gn_ext_ws_contracts_datamember;
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.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for ArrayOfReturnMessageDetail complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="ArrayOfReturnMessageDetail">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="ReturnMessageDetail" type="{http://schemas.datacontract.org/2004/07/GN.Ext.WS.Contracts.DataMember.Common}ReturnMessageDetail" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfReturnMessageDetail", propOrder = {
"returnMessageDetail"
})
public class ArrayOfReturnMessageDetail {
@XmlElement(name = "ReturnMessageDetail", nillable = true)
protected List<ReturnMessageDetail> returnMessageDetail;
/**
* Gets the value of the returnMessageDetail 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 returnMessageDetail property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getReturnMessageDetail().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link ReturnMessageDetail }
*
*
*/
public List<ReturnMessageDetail> getReturnMessageDetail() {
if (returnMessageDetail == null) {
returnMessageDetail = new ArrayList<ReturnMessageDetail>();
}
return this.returnMessageDetail;
}
}
Building the return message
ReturnMessage retMsg = new ReturnMessage();
retMsg.setReturnCode(0);
retMsg.setReturnText("Success");
ReturnMessageDetail[] retMsgDetArr = new ReturnMessageDetail[1];
ReturnMessageDetail retMsgDet = new ReturnMessageDetail();
retMsgDet.setMessageType(new JAXBElement(new QName("MessageType"), String.class, "I"));
retMsgDet.setMessageText(new JAXBElement(new QName("MessageText"), String.class, "SUCCESS"));
retMsgDetArr[0] = retMsgDet;
//Set the return message details
retMsg.setReturnMessageDetails(new JAXBElement(new QName("http://schemas.datacontract.org/2004/07/GN.Ext.WS.Contracts.DataMember.Common","ReturnMessageDetail"),ReturnMessage.class,null, retMsgDetArr[0]));
This returns:
<ns4:ReturnMessage>
<ReturnCode>0</ReturnCode>
<ReturnText>Success</ReturnText>
<ReturnMessageDetail>
<MessageType>I</MessageType>
<MessageText>SUCCESS</MessageText>
</ReturnMessageDetail>
</ns4:ReturnMessage>
I need it to return:
<ns4:ReturnMessage>
<ReturnCode>0</ReturnCode>
<ReturnText>Success</ReturnText>
<ReturnMessageDetails>
<ReturnMessageDetail>
<MessageType>I</MessageType>
<MessageText>SUCCESS</MessageText>
</ReturnMessageDetail>
</ReturnMessageDetails>
</ns4:ReturnMessage>
How should I call and set all three of these packages to get the correct XML format?
Thanks for the help.