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
}
Solved! Go to Solution.
Views
Replies
Total Likes
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!
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!
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
Views
Replies
Total Likes
@AmitVishwakarma What's the difference in your answer than @SantoshSai posted? Seems you are wasting your energy here by copy pasting answers from ChatGpt!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies