I'm working on a project with JAXB but I run into a small problem with JAXB and the char datatype.
char gender = 'M';
Translates after marshalling into:
<gender>77</gender>
So I think that char is mapped to integer, but I simply want to map it to a String. How can I do this? Is it even possible?
After some experimentation, there appears to be no way to configure JAXB to handle primitive chars properly. I'm having a hard time accepting it, though.
I've tried defining an XmlAdaptor
to try and coerce it into a String, but the runtime seems to only accept adapters annotated on Object types, not primitives.
The only workaround I can think of is to mark the char field with @XmlTransient
, and then write getters and setters which get and set the value as a String:
@XmlTransient
char gender = 'M';
@XmlElement(name="gender")
public void setGenderAsString(String gender) {
this.gender = gender.charAt(0);
}
public String getGenderAsString() {
return String.valueOf(gender);
}
Not very nice, I'll grant you, but short of actually changing your char field tobe a String, that's all I have.