Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Use of component exporter

Avatar

Level 4

to export model I'm using Jackson exporter so json is coming for my model as expected.

But I observed few of the model  class again they are implements CompoentExporter?

What is the  use if  class implements CompoentExporter?

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
  • @exporter(name="Jackson", type="json"):
    This appears to be an annotation used in Java, possibly within a framework or library.
  • The @exporter annotation with parameters name and type suggests that it's specifying an exporter configuration, possibly for serializing Java objects to JSON format using the Jackson library.
  • The name parameter likely specifies the name of the exporter, and type specifies the type of data format or serializer being used (in this case, JSON).
  • This annotation is used to customize the behavior of serialization, particularly when using Jackson for JSON serialization in Java.

 

com.adobe.cq.export.json.ComponentExporter:

  • This is an interface provided by Adobe Experience Manager (AEM).
  • It's used in the context of AEM development, particularly for exporting component data to JSON format.
    Implementing this interface allows AEM components to be serialized as JSON objects, typically for consumption in Single Page Applications (SPAs) or other client-side rendering scenarios.
  • This interface defines methods such as getExportedType() and exportAsJson(), which components must implement to specify the exported type and provide JSON representation of the component data.

In summary, the @exporter(name="Jackson", type="json") annotation is more about configuring serialization behavior in Java using the Jackson library, while com.adobe.cq.export.json.ComponentExporter is specifically about exporting AEM component data to JSON format for client-side rendering within the AEM ecosystem.

View solution in original post

3 Replies

Avatar

Community Advisor

@user96222 In the Adobe Experience Manager (AEM) framework, the ComponentExporter interface is used to export a component's data to be consumed by client-side code, typically for rendering purposes on the client side. This interface is part of the AEM SPA Editor and AEM's support for Single Page Applications (SPAs) where components are rendered on the client side using frameworks like React, Angular, or Vue.js.

When a class implements the ComponentExporter interface, it means that the class is responsible for providing the necessary data to render a component on the client side. This data could include component properties, content, state, and any other relevant information required for rendering.

Implementing ComponentExporter involves implementing the exportAsJson() method, which returns a JSON representation of the component's data. This JSON data can then be consumed by client-side JavaScript to render the component.

Here's a basic example of how a class might implement ComponentExporter in Java code within the AEM context:

 

import com.adobe.cq.export.json.ComponentExporter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class MyComponent implements ComponentExporter {

    private Resource resource;

    public MyComponent(Resource resource) {
        this.resource = resource;
    }

    @Override
    public String getExportedType() {
        return "myproject/components/mycomponent";
    }

    @Override
    public Object exportAsJson() throws IOException {
        ValueMap properties = resource.getValueMap();
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(properties);
    }
}

 

 

In this example:

  • MyComponent is a Java class representing a component.
  • It implements the ComponentExporter interface.
  • The getExportedType() method specifies the resource type of the component.
  • The exportAsJson() method retrieves the component's properties as a ValueMap and converts it to a JSON string using an ObjectMapper.

By implementing ComponentExporter, this class enables AEM to export the component's data as JSON, which can then be consumed by client-side code for rendering the component.

Avatar

Level 4

 @exporter(name="Jackson",type="json")

Even above exporter also generate json for my mode

Then what is diff  between both?

Avatar

Correct answer by
Community Advisor
  • @exporter(name="Jackson", type="json"):
    This appears to be an annotation used in Java, possibly within a framework or library.
  • The @exporter annotation with parameters name and type suggests that it's specifying an exporter configuration, possibly for serializing Java objects to JSON format using the Jackson library.
  • The name parameter likely specifies the name of the exporter, and type specifies the type of data format or serializer being used (in this case, JSON).
  • This annotation is used to customize the behavior of serialization, particularly when using Jackson for JSON serialization in Java.

 

com.adobe.cq.export.json.ComponentExporter:

  • This is an interface provided by Adobe Experience Manager (AEM).
  • It's used in the context of AEM development, particularly for exporting component data to JSON format.
    Implementing this interface allows AEM components to be serialized as JSON objects, typically for consumption in Single Page Applications (SPAs) or other client-side rendering scenarios.
  • This interface defines methods such as getExportedType() and exportAsJson(), which components must implement to specify the exported type and provide JSON representation of the component data.

In summary, the @exporter(name="Jackson", type="json") annotation is more about configuring serialization behavior in Java using the Jackson library, while com.adobe.cq.export.json.ComponentExporter is specifically about exporting AEM component data to JSON format for client-side rendering within the AEM ecosystem.