users@jersey.java.net

[Jersey] Re: _at_XmlEnumValue is not recognized by Jackson

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Wed, 6 Apr 2011 09:38:34 -0700

On Wed, Apr 6, 2011 at 7:00 AM, Pengfei Di <pengfei.di_at_match2blue.com> wrote:
> Hello,
>
> I am trying to write a JAXB class, that constains a field of enum type, into
> JSON String. I tried with @XmlEnumValue annotation, but it doesn't take
> effect.
> The JAXB class looks like:
>
> @XmlRootElement
> public class MyClass {
>
>    @XmlEnum(Integer.class)
>    public enum MyEnum {
>        @XmlEnumValue("1")        INACTIVE,
>        @XmlEnumValue("2")        ACTIVE
>    }
>
>    private MyEnum myEnum;
>
>    public getMyEnum(){
>        return myEnum;
>    }
>    // setter
>    ...
> }
>
> The expected JSON string is : {"myEnum":2}
> But what I get is : {"myEnum":"ACTIVE"}
>
> Does Jackson support this @XmlEnum annotation? or did I do something wrong?

Jackson does not support @XmlEnum -- enums will always be serialized
as Strings, unless custom serializer is used -- but
JaxbAnnotationIntrospector should support @XmlEnumValue for
customizing actual values used. So this looks like a bug, as long as
you have JAXB annotation support enabled (if not, you do need to
enable support).

-+ Tatu +-