Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to display JSON data through Sling Model

Avatar

Avatar
Boost 1
Level 1
ashwinikhaple
Level 1

Like

1 like

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Boost 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 1
Level 1
ashwinikhaple
Level 1

Like

1 like

Total Posts

16 posts

Correct Reply

1 solution
Top badges earned
Boost 1
Applaud 5
Affirm 1
View profile
ashwinikhaple
Level 1

06-04-2021

I have created a Component that has title, description, date, URL, and read the values in SlingModel then how can I display the data in the form of JSON to the frontend through the sling model?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

551 likes

Total Posts

663 posts

Correct Reply

191 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

551 likes

Total Posts

663 posts

Correct Reply

191 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile
asutosh_jena
Level 10

06-04-2021

Hi @ashwinikhaple 
If there is a sling model available and you want to fetch the same properties as a JSON response,so there is no need to create a Sling Servlet. You just need to export your sling model using jackson exporter and that’s all. Sling Model Exporter can be used as a web service or as a rest API.

 

Sling Model Exporter was introduced in Sling Models v1.3.0 which allows new annotations to be added to Sling Models that define how the Model an can be exported as a different Java object, or more commonly, serialized into a different format such as JSON.

 

@Model(adaptables = Resource.class, resourceType = { "wknd/components/content/image" }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = "jackson", extensions = "json", options = { @ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "true") })
public class ModelComponent {

@Inject
String title;

@Inject
String description;

@Inject
String date;

@Inject
String URL;

@Inject
@Named("sling:resourceType")
String slingResourceType;

public String getDescription() {
return description;
}

public String getDate() {
return date;
}

public String getURL() {
return URL;
}

public String getSlingResourceType() {
return slingResourceType;
}

public String getTitle() {
return title;
}
}

 

Please refer the below article:

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/develop-slin...

http://www.sgaemsolutions.com/2017/06/sling-model-exporter-in-aem-63.html

 

Hope this helps!

Thanks! 

Answers (1)

Answers (1)

Avatar

Avatar
Establish
Level 4
Rohit_Utreja
Level 4

Likes

44 likes

Total Posts

45 posts

Correct Reply

15 solutions
Top badges earned
Establish
Give Back 3
Give Back
Boost 5
Boost 3
View profile

Avatar
Establish
Level 4
Rohit_Utreja
Level 4

Likes

44 likes

Total Posts

45 posts

Correct Reply

15 solutions
Top badges earned
Establish
Give Back 3
Give Back
Boost 5
Boost 3
View profile
Rohit_Utreja
Level 4

06-04-2021