Why is this Enum class not JAXB-marshalled?

33 Views Asked by At

I have a Java 8 application using JAXB with MOXy 2.3.2 , and it is working fine with all properties except for an Enum that was introduced recently: UserProfileExtensionGenderEnum.java, here is the JAXB object that gets marshalled into an XML.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="root", namespace=ZGXMLHelper.ZWISCHENGAS_NS)
public class UserProfileExtension {
    @XmlElement(name="emailVerifyReminder2Sent", namespace=ZGXMLHelper.ZWISCHENGAS_NS)
    private boolean emailVerifyReminder2Sent = Boolean.FALSE;
    ...
    @XmlElement(name="gender", namespace=ZGXMLHelper.ZWISCHENGAS_NS)
    private UserProfileExtensionGenderEnum gender = UserProfileExtensionGenderEnum.NOTSET; 

}

The Enum:

package com.zwischengas.model.jaxb;

import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;

@XmlEnum
public enum UserProfileExtensionGenderEnum {

    @XmlEnumValue("m")
    MALE, 
    @XmlEnumValue("f")
    FEMALE, 
    @XmlEnumValue("notset")
    NOTSET;
    
}

All JAXB classes are in the same package, and in that package there is this package-into.java:

@javax.xml.bind.annotation.XmlSchema( 
    namespace = "http://zwischengas.com/article/1.0/", 
    xmlns = {@javax.xml.bind.annotation.XmlNs(prefix = "z", namespaceURI ="http://zwischengas.com/article/1.0/"),@javax.xml.bind.annotation.XmlNs(prefix = "y", namespaceURI ="http://www.wyona.org/yanel/1.0")},  
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) 
package com.zwischengas.model.jaxb; 

What is missing in order to have this enum marshalled too?

1

There are 1 best solutions below

3
Laurent Schoelens On

On Java enum generated by jaxb-maven-plugin, I generally have an @XmlType(name = "myAwesomeEnum") annotation on the enum with the @XmlEnum annotation like the following example :

@XmlType(name = "myAwesomeEnum")
@XmlEnum
public enum MyAwesomeEnum {

    FIRST_ENUM("FIRST_ENUM"),
    SECOND_ENUM("SECOND_ENUM"),
    @XmlEnumValue("THIRD3_ENUM")
    THIRD_3_ENUM("THIRD3_ENUM")
}

@XmlEnumValue is only necessary if name of the enum differs from value (like 3rd enum name)

I'd try to add the @XmlType(name = "userProfileExtensionGenderEnum") on your enum to see if it fixes your problem.