@XmlRootElement (jaxb)

The @XmlRootElement annotation provides an alternative way to define a view model, in particular one intended to act as a DTO for use within RestfulObjects viewer, or which contains arbitrarily complex state.

A view model is a non-persisted domain object whose state is converted to/from a string memento. In the case of a JAXB-annotated object this memento is its XML representation. JAXB generally requires that the root element of the XML representation is annotated with @XmlRootElement. Apache Isis makes this a mandatory requirement.

In comparison to using either the ViewModel interface or the @DomainObject(nature=VIEW_MODEL) annotation, using @XmlRootElement has a couple of significant advantages:

  • the view model can be used as a "canonical" DTO, for example when accessing data using the RestfulObjects viewer in combination with the ContentMappingService.

    This provides a stable and versioned API to access data in XML format using whatever client-side technology may be appropriate.

  • the XML graph can be as deep as required; in particular it can contain collections of other objects.

    In contrast, if the @DomainObject(nature=VIEW_MODEL) annotation is used then only the state of the properties (not collections) is captured. If using ViewModel interface then arbitrary state (including that of collections), however the programmer must write all the code by hand

The main disadvantages of using JAXB-annotated view models is that any referenced persistent entity must be annotated with the @XmlJavaTypeAdapter, using the framework-provided PersistentEntityAdapter. This adapter converts any references to such domain entities using the oidDto complex type (as defined by the Apache Isis common schema): the object’s type and its identifier.

The memento string for view models is converted into a form compatible with use within a URL. This is performed by the UrlEncodingService, the default implementation of which simply encodes to base 64. If the view model XML graph is too large to be serialized to a string, then an alternative implementation (eg which maps XML strings to a GUID, say) can be configured using the technique described in here in the user guide.


For example:

@XmlRootElement(name = "toDoItemDto")           (1)
public class ToDoItemDto implements Dto {
    @Getter @Setter                             (2)
    protected String description;
    @Getter @Setter
    protected String category;
    @Getter @Setter
    protected String subcategory;
    @Getter @Setter
    protected BigDecimal cost;
1 identifies this class as a view model and defines the root element for JAXB serialization
2 using Project Lombok for getters and setters

See also

Although (like any other viewmodel) a JAXB-annotated can have behaviour (actions) and UI hints, you may wish to keep the DTO "clean", just focused on specifying the data contract.

Behaviour can therefore be provided using mixins (annotated with @DomainObject(nature=MIXIN)), while UI events can be used to obtain title, icons and so on.

For a more complete discussion of writing JAXB view models/DTOs, see this topic in the user guide.