[#javax-xml-bind-annotation-adapters-XmlRootElement]
= `@XmlRootElement` (`jaxb`)

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


The `@XmlRootElement` annotation provides an alternative way to define a xref:userguide:fun:overview.adoc#view-models[view model], in particular one intended to act as a DTO for use within xref:vro:ROOT:about.adoc[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 xref:refguide:applib:index/ViewModel.adoc[ViewModel] interface or the
xref:refguide:applib:index/annotation/DomainObject.adoc[@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 xref:vro:ROOT:about.adoc[RestfulObjects viewer] in combination with the
xref:refguide:applib:index/services/conmap/ContentMappingService.adoc[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 xref:refguide:applib-ant:XmlJavaTypeAdapter.adoc[@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 xref:refguide:schema:common.adoc[common schema]): the object's type and its identifier.

[TIP]
====
The memento string for view models is converted into a form compatible with use within a URL.
This is performed by the xref:refguide:applib:index/services/urlencoding/UrlEncodingService.adoc[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 xref:userguide:btb:hints-and-tips/replacing-default-service-implementations.adoc[here] in the user guide.
====

== Example

For example:

[source,java]
----
@XmlRootElement(name = "toDoItemDto")           // <.>
public class ToDoItemDto implements Dto {
    @Getter @Setter                             // <.>
    protected String description;
    @Getter @Setter
    protected String category;
    @Getter @Setter
    protected String subcategory;
    @Getter @Setter
    protected BigDecimal cost;
}
----
<.> identifies this class as a view model and defines the root element for JAXB serialization
<.> 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 xref:userguide:fun:overview.adoc#mixins[mixins] (annotated with xref:refguide:applib:index/annotation/DomainObject.adoc[@DomainObject(nature=MIXIN)]), while xref:applib-classes:events.adoc#ui-event-classes[UI events] can be used to obtain title, icons and so on.

For a more complete discussion of writing JAXB view models/DTOs, see xref:userguide:fun:overview.adoc#view-models[this topic] in the user guide.

