Reputation: 69
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "AccountResponse")
public class AccountResponse {
@XmlElement(name = "AccountNumber")
protected long accountNumber;
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long value) {
this.accountNumber = value;
}
}
In above mentioned code snippet, I am trying to decorate accountNumber with AccountNumber using @XmlElement annotation.Code is getting executed without throwing any exceptions. But not getting expected output. How to solve this problem?
Expected Output :
<AccountResponse>
<AcconutNumber>1234</AccountNumber>
</AccountResponse>
Actual Output(that I am getting) :
<AccountResponse>
<acconutNumber>1234</accountNumber>
</AccountResponse>
NOTE : I am using a jar for JAXB from Mule Runtime Library 3.4 .
Thanks in Advance!
Upvotes: 2
Views: 2712
Reputation: 8427
What you are doing is almost correct in my opinion, but you should try to use your annotation on getter instead of Field:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "AccountResponse")
public class AccountResponse {
protected long accountNumber;
@XmlElement(name = "AccountNumber")
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long value) {
this.accountNumber = value;
}
}
Depending on your configuration, xml tag name may be created from your getter instead of your field name. In this case I guess it happened.
Upvotes: 1
Reputation: 149047
Running the following code on the AccountResponse
class from your question:
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(AccountResponse.class);
AccountResponse ar = new AccountResponse();
ar.setAccountNumber(1234);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(ar, System.out);
}
}
Will get you the XML response that you are expecting:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccountResponse>
<AccountNumber>1234</AccountNumber>
</AccountResponse>
Determining the Problem
AccountResponse
class then the issue is with Mule.AccountResponse
class may not have been recompiled since you added the @XmlElement(name = "AccountNumber")
annotation.Upvotes: 0