您查看的帮助内容对应的版本是:

Components can be adapted to generate JSON export of their content based on a modeler framework.

Overview

The JSON Export is based on Sling Models, and on the Sling Model Exporter framework (which itself relies on Jackson annotations).

This means that the component must have a Sling Model if it needs to export JSON. Therefore you will need to follow these two steps to enable JSON export on any component.

Define a Sling Model for the Component

First a Sling Model must be defined for the component.

注意:

For an example of using Sling Models see the article Developing Sling Model Exporters in AEM.

The Sling Model implementation class must be annotated with the following:

@Model(... adapters = {..., ComponentExporter.class})
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
@JsonSerialize(as = MyComponent.class)

This ensures that your component could be exported on its own, using the .model selector and the .json extension.

In addition, this specifies that the Sling Model class can be adapted into the ComponentExporter interface.

注意:

Jackson annotations are not usually specified at the Sling Model class level, but rather at the Model interface level. This is to ensure that the JSON Export is considered as part of the component API.

注意:

The ExporterConstants and ComponentExporter classes come from the com.adobe.cq.export.json bundle.

Annotate the Sling Model Interface

To be taken into account by the JSON Exporter framework, the Model interface should implement the ComponentExporter interface (or ContainerExporter, in the case of a container component).

The corresponding Sling Model interface (MyComponent) would be then annotated using Jackson annotations to define how it should be exported (serialized).

The Model interface needs to be properly annotated to define which methods should be serialized. By default, all methods that respect the usual naming convention for getters will be serialized and will derive their JSON property names naturally from the getter names. This can be prevented or overridden using @JsonIgnore or @JsonProperty to rename the JSON property.

Example

The Core Components have supported JSON export since release 1.1.0 of the core components and can be used as a reference.

For an example, see the Sling Model implementation of the Image Core Component and its annotated interface.

CODE ON GITHUB

You can find the code of this page on GitHub

Related Documentation

本产品经 Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License 许可  Twitter™ 与 Facebook 中的内容不在 Creative Commons 的条款约束之下。

法律声明   |   在线隐私策略