Reputation: 53
I use Sprint 3.0.5 and the included jaxb marshaller to communicate with a REST service. The service provided by a different company sends via POST an XML to my Service and i have to unmarshal that XML in my Java objects and process them.
The problem i have is, that the XML tags start with a capital letter (they wont change that) and therefore the JAXB marshaller cant unmarshal the object.
The XML looks like the following:
<Ssm_Packet>
<Version>1</Version>
<Protocol_ID>0</Protocol_ID>
<Packet_Id>{84ca597c-05e2-4357-897c-892f428c35ce}</Packet_Id>
<Priority>0</Priority>
<Source_Address>1:11111111111111</Source_Address>
<Destination_Address>2:LA3222222222222</Destination_Address>
<Body>Some TExt</Body>
<Billing />
</Ssm_Packet>
The bean I defined for jaxb looks like the following:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Ssm_Packet")
public class Ssm_Packet {
@XmlElement(name="Version")
private String version;
private String protocol_ID;
private String packet_Id;
private String priority;
private String source_Address;
private String destination_Address;
private String body;
private String billing;
/**
* @return the version
*/
public String getVers() {
return version;
}
/**
* @param version the version to set
*/
public void setVers(String Version) {
this.version = Version;
}
/**
* @return the protocol_ID
*/
public String getProtocol_ID() {
return protocol_ID;
}
/**
* @param protocol_ID the protocol_ID to set
*/
public void setProtocol_ID(String Protocol_ID) {
this.protocol_ID = Protocol_ID;
}
/**
* @return the packet_Id
*/
public String getPacket_Id() {
return packet_Id;
}
/**
* @param packet_Id the packet_Id to set
*/
public void setPacket_Id(String Packet_Id) {
this.packet_Id = Packet_Id;
}
/**
* @return the priority
*/
public String getPriority() {
return priority;
}
/**
* @param priority the priority to set
*/
public void setPriority(String Priority) {
this.priority = Priority;
}
/**
* @return the source_Address
*/
public String getSource_Address() {
return source_Address;
}
/**
* @param source_Address the source_Address to set
*/
public void setSource_Address(String Source_Address) {
this.source_Address = Source_Address;
}
/**
* @return the destination_Address
*/
public String getDestination_Address() {
return destination_Address;
}
/**
* @param destination_Address the destination_Address to set
*/
public void setDestination_Address(String Destination_Address) {
this.destination_Address = Destination_Address;
}
/**
* @return the body
*/
public String getBody() {
return body;
}
/**
* @param body the body to set
*/
public void setBody(String Body) {
this.body = Body;
}
/**
* @return the billing
*/
public String getBilling() {
return billing;
}
/**
* @param billing the billing to set
*/
public void setBilling(String Billing) {
this.billing = Billing;
}
}
Now if i let JAXb unmarshal the XML in that object, he wont fill in the values of the xml unless the xml tags are without a capital letter.
Can anyone help me how to unmarshal that values into my bean?
thx
Upvotes: 3
Views: 7696
Reputation: 149017
You can use the @XmlElement
annotation to specify the element name that corresponds to each of your fields/properties.
/**
* @return the protocol_ID
*/
@XmlElement(name="Protocol_ID")
public String getProtocol_ID() {
return protocol_ID;
}
If you want to annotate your fields instead, then you will need to set @XmlAccessorType(XmlAccessType.FIELD)
:
@XmlRootElement(name="Ssm_Packet")
@XmlAccessorType(XmlAccessType.FIELD)
public class Ssm_Packet {
@XmlElement(name="Version")
private String version;
}
EclipseLink JAXB (MOXy) also contains an extension where you can override the standard JAXB algorithm for converting Java field/property names to XML names:
Upvotes: 5