I am using JAXB (the version included with JDK 6) to marshall objects to XML. The following piece of code yields unexpected results:
public class JAXBTest {
@XmlRootElement
public static class VIPPerson {}
public static void main(String[] args) throws JAXBException {
StringWriter sw = new StringWriter();
VIPPerson p = new VIPPerson();
JAXB.marshal(p, sw);
System.out.println(sw.toString());
}
}
The output from the above is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vipPerson/>
I am expecting to see the class name mapped to the VIPPerson
element rather than vipPerson
based on section 8.12.1 in the JAXB specification, which says
class name: a class name is mapped to an XML name by de capitalization using java.beans.Introspector.decapitalize(class name ).
The JavaDoc for that decapitalize
method says this:
Utili开发者_开发技巧ty method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone. Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".
Does the implementation violate the spec or am I misunderstanding something?
This is caused by a bug in the reference implementation. Looks like it won't be fixed due to the compatibility problems it would cause. The workaround is to explicitly specify a name with @XmlRootElement
.
精彩评论