Reputation: 15389
This is something I encountered as a real world problem.
I have a class as shown below. The choice of the field names is not mine, but is dictated by actual field names in the database (names changed).
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement(name = "JAXBAnnotatedClass1")
@XmlType(propOrder = { "A_DT", "B_DT" })
public class JAXBAnnotatedClass1
{
private Date A_DT;
private Date B_DT;
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
public Date getA_DT()
{
return A_DT;
}
public void setA_DT(Date a_DT)
{
A_DT = a_DT;
}
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
public Date getB_DT()
{
return B_DT;
}
public void setB_DT(Date b_DT)
{
B_DT = b_DT;
}
}
I don't think the Date Adapter class is relevant to the problem.
I am using Eclipse Indigo Service Release Version 1. I tried to generate schema from this class, but I got the following errors -
Property a_DT is present but not specified in @XmlType.propOrder
this problem is related to the following location:
....JAXBAnnotatedClass1.getA_DT()
at com.cigna.framework.testing.JAXBAnnotatedClass1
Property b_DT is present but not specified in @XmlType.propOrder
this problem is related to the following location:
....JAXBAnnotatedClass1.getB_DT()
The weird thing here is that if I make the following change, everything works -
@XmlType(propOrder = { "a_DT", "b_DT" }) // changed first uppercase letter
//to lowercase without changing field name
Another interesting observation is that if I had field names like these (below) instead, everything works! The only difference in the case below is that the field name has two uppercase letters instead of one, before the underscore.
@XmlRootElement(name = "JAXBAnnotatedClass2")
@XmlType(propOrder = { "AX_DT", "BX_DT" })
public class JAXBAnnotatedClass2
{
private Date AX_DT;
private Date BX_DT;
// similar code...
What is causing this problem? Is there a way to solve this?
Upvotes: 1
Views: 2032
Reputation: 15389
The propOrder will have to lowercase the first letter. The name of the XMLElement can be changed with the @XmlElement annotation and everything works fine.
Changes -
@XmlType(propOrder = { "a_DT", "b_DT" })
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
@XmlElement(name = "A_DT")
public Date getA_DT()
{
return A_DT;
}
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
@XmlElement(name = "B_DT")
public Date getB_DT()
{
return B_DT;
}
Upvotes: 0
Reputation: 149027
The first thing to note is that by default your JAXB (JSR-222) implementation is processing the public properties and not the private fields. This means it doesn't matter what you call your field since JAXB will be looking at the property names (see code sample below). JAXB will lower case the first letter in the property name (getA_DT
corresponds to the property a_DT
), unless there are two capital letters in a row (getAX_DT
corresponds to the property AX_DT
).
package forum12304863;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder={"a_DT", "b_DT", "AX_DT", "BX_DT"})
public class Root {
private String field1;
private String field2;
private String field3;
private String field4;
public String getA_DT() {
return field1;
}
public void setA_DT(String a_DT) {
field1 = a_DT;
}
public String getB_DT() {
return field2;
}
public void setB_DT(String b_DT) {
field2 = b_DT;
}
public String getAX_DT() {
return field3;
}
public void setAX_DT(String aX_DT) {
field3 = aX_DT;
}
public String getBX_DT() {
return field4;
}
public void setBX_DT(String bX_DT) {
field4 = bX_DT;
}
}
For More Information
Upvotes: 2
Reputation: 53694
When a getter is named "getFoo", jaxb will by default name the element "foo". i think you should be able to fix the problem by explicitly defining the element names, e.g.:
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
@XmlElement(name = "A_DT")
public Date getA_DT()
{
return A_DT;
}
Upvotes: 1