Skip to main content
Level 2
Apr 29, 2025 10:12 PM UTC
Solved

AEM Sling Model Jackson Exporter returns null for nested objects

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 }

 

SantoshSai
SantoshSaiAccepted solution
Community Advisor
Apr 29, 2025 10:44 PM UTC

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

3 replies

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
Apr 29, 2025 10:44 PM UTC

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!

Translate
Santosh Sai
AmitVishwakarma
Community Advisor
Community Advisor
Apr 30, 2025 9:56 AM UTC

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

Translate
Amit Vishwakarma - Adobe Commerce Champion 2025 | 17x Adobe certified | 6x Adobe SME
Level 2
Apr 30, 2025 7:02 PM UTC

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

 
Translate