xmlxml-parsingjacksonjaxbxml-binding

@XmlRootElement vs @JacksonXmlRootElement


What is exact difference between @XmlRootElement @JacksonXmlRootElement?

I know @XmlRootElement is generic, But when I check package structure both are different.

So there must be some advantage and disadvantage while using this.


Solution

  • JacksonXmlRootElement allow to define name of root level

    define name of root element used for the root-level object when serialized

    Here's the reason for adding @JacksonXmlRootElement

    JAXB allows you to override the name of the root element (using @XmlRootElement when generating XML. For example, rendering this class to XML:

    @JsonIgnoreProperties( {"meal", "notMeal", "mealNameDisplay"} ) public class MealEvent...

    Generates XML like this:

    lunch ... But I'd like the root element to be 'meal' instead of 'MealEvent'.

    FWIW, I tried the wild guess using @JsonTypeName( "meal" ) as well – it had no effect.

    Implemented: now there is @JacksonXmlRootElement (named similar to JAXB annotation, just with "Jackson" prefix to reduce confusion).