How to display JSON data through Sling Model | Adobe Higher Education
Skip to main content
Level 2
April 6, 2021
해결됨

How to display JSON data through Sling Model

  • April 6, 2021
  • 2 답변들
  • 4604 조회

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?

이 주제는 답변이 닫혔습니다.
최고의 답변: Asutosh_Jena_

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-sling-model-exporter.html?lang=en#configuring-aem-for-sling-model-exporter

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

 

Hope this helps!

Thanks! 

2 답변

Asutosh_Jena_
Community Advisor
Community Advisor
April 6, 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-sling-model-exporter.html?lang=en#configuring-aem-for-sling-model-exporter

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

 

Hope this helps!

Thanks! 

Rohit_Utreja
Community Advisor
Community Advisor
April 6, 2021