AEM Sling Model Jackson Exporter returns null for nested objects | Community
Skip to main content
April 29, 2025
Solved

AEM Sling Model Jackson Exporter returns null for nested objects

  • April 29, 2025
  • 2 replies
  • 569 views

Hi all,

I’m trying to expose a Sling model using the Jackson exporter in AEM as a Cloud Service, but I’m facing an issue where nested objects or lists return null in the JSON output, even though the values are present in the JCR.

Here's a simplified version of my code:

Sling Model

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = "myproject/components/article", adapters = ArticleModel.class) @Exporter(name = "jackson", extensions = "json") public class ArticleModel { @ValueMapValue private String title; @ValueMapValue private String description; @ChildResource private List<AuthorModel> authors; // getters... }

 Nested Model

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class AuthorModel { @ValueMapValue private String name; @ValueMapValue private String role; // getters... }

Expected Output

{ "title": "Sample Article", "description": "A test article", "authors": [ { "name": "Jane Doe", "role": "Editor" } ] }

 Actual Output

{ "title": "Sample Article", "description": "A test article", "authors": null }

 

Best answer by SantoshSai

Hi @matthewda19,

I think you missed adding Jackson exporter annotations on the nested model (AuthorModel)

@Exporter(name = "jackson", extensions = "json")

Nested models need the @Exporter annotation because Sling’s Jackson exporter only serializes models explicitly registered for export. Without it, the nested object isn’t recognized as exportable and will be skipped or serialized as null.

For more detailed information, you can refer to the official documentation:

Hope that helps!

2 replies

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
April 29, 2025

Hi @matthewda19,

I think you missed adding Jackson exporter annotations on the nested model (AuthorModel)

@Exporter(name = "jackson", extensions = "json")

Nested models need the @Exporter annotation because Sling’s Jackson exporter only serializes models explicitly registered for export. Without it, the nested object isn’t recognized as exportable and will be skipped or serialized as null.

For more detailed information, you can refer to the official documentation:

Hope that helps!

Santosh Sai
AmitVishwakarma
Community Advisor
Community Advisor
April 30, 2025

Hi @matthewda19 ,

Your nested model (AuthorModel) is missing the @Exporter annotation, which is required for Sling to serialize it properly using Jackson.

Update your nested model like this:

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) @Exporter(name = "jackson", extensions = "json") // ✅ THIS IS REQUIRED public class AuthorModel { @ValueMapValue private String name; @ValueMapValue private String role; // getters... }

Why this works:

Sling’s Jackson exporter only serializes classes explicitly registered with @Exporter. Without it, nested objects (like List<AuthorModel>) return null.

After Fix – Output Will Be:

{ "title": "Sample Article", "description": "A test article", "authors": [ { "name": "Jane Doe", "role": "Editor" } ] }

 

Regards,
Amit

April 30, 2025

@AmitVishwakarma What's the difference in your answer than @SantoshSai posted? Seems you are wasting your energy here by copy pasting answers from ChatGpt!