I’m new to all the JAXB/JAX-RS stuff. At work, we are using Mule ESB with its Jersey/Jackson module to accept incoming REST requests. On the client side we’re using RestEasy (with Jackson)… The request should be in Json, not in XML.
It works well for simple objects, containing String fields only. But as soon as we start using more “complex” types, like Locales, enums or Maps, problems arise.
If I understand correctly, there is no built-in serializers in JAXB for types like Map (I should even say “HashMap”, since JAXB doesn’t support interfaces well if I understand correctly). So you have to provide your own serializers for those fields.
From my searches on the web, I’ve seen that the @XmlJavaTypeAdapter annotation can be used to manage your own serialization of a type like “Map”. But I also see, in Jackson documentation, that the @JsonSerialize(using=MySerializer.class) annotation can be used.
What should we use and why? @XmlJavaTypeAdapter or @JsonSerialize? Are they the same or do they have different purpose?
First of all: Jackson fully supports all JDK structured types, including Maps, so no special configuration or annotations are usually needed. Jackson is not a JAXB implementation or limited by JAXB limitations (which are based on mapping problems between POJO and XML, which
are somewhat bigger than those with JSON).
Jackson also supports interfaces well; for serialization you seldom need anything additional.
For deserialiation you may or may not need; and there are multiple ways to indicate concrete implementations; but you usually do not need to define custom serializers or deserializers.
So it is possible that answer is that you do not need either.
Now: JAXB support is an optional compatibility thing for Jackson, mostly meant for supporting legacy things where JAXB is or was the thing used for XML. Jackson’s own annotations are the preferred mechanism, if you do not start with JAXB annotations.
So unless you need JAXB annotations for other purposes (reading/writing XML), I would suggest just using Jackson’s @JsonSerialize, if annotation-based configuration is indeed needed.