Need Jackson Exporter JSON response within SlingModel Class
Hi Team,
Usecase: Need to utilize model JSON response in the component sightly file.
Basically, we use Jackson Exporter to get JSON response for SlingModel but it's a servlet call on the component resource. But my requirement is to get the JSON response for SlingModel from one of the getter functions of the Sling Model.
something like -
@Model(adaptables = {
SlingHttpServletRequest.class
}, adapters = {
Button2.class,
ComponentExporter.class
}, resourceType = Button2Impl.RESOURCE_TYPE)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class Button2Impl implements Button2 {
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String label;
public String getJsonString() {
String json = "logic to get model json response like sling model exporter;
return json;
}
}<sly data-sly-use.buttonProvider="${'com.sample.general.models.Button2'}">
<span> ${buttonProvider.jsonString} </span>
</sly>
Could anyone suggest how to achieve the above scenario?
Thanks,
Aman
SlingModel Implementation Class -
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sample.sample.data_providers.general.models.Button2;
import com.sample.sample.services.utils.LinkUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.apache.sling.models.factory.ExportException;
import org.apache.sling.models.factory.MissingExporterException;
import org.apache.sling.models.factory.ModelFactory;
import java.util.HashMap;
import java.util.Map;
@Model(adaptables = {
SlingHttpServletRequest.class
}, adapters = {
Button2.class,
ComponentExporter.class
}, resourceType = Button2Impl.RESOURCE_TYPE)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class Button2Impl
implements Button2
{
public static final String RESOURCE_TYPE = "sample/components/general/button-2-0";
private ResourceResolverFactory resourceResolverFactory;
@SlingObject
private ResourceResolver resourceResolver;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String iconAlignment;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String buttonAlignment;
@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
private String marginBottom;
@SlingObject
private Resource resource;
@SlingObject
private SlingHttpServletRequest request;
@OSGiService
ModelFactory modelFactory;
public String getIconAlignment() { return iconAlignment; }
public String getButtonAlignment() { return buttonAlignment; }
@JsonIgnore
public String getMarginBottom() { return marginBottom; }
public String getExportedType() {
return resource.getResourceType();
}
public String getJsonString() {
Button2 buttonModel = request.adaptTo(Button2Impl.class); // tried adapting with resource as well but no luck
Map<String,String> options = new HashMap();
String s = null;
try {
s = modelFactory.exportModel(buttonModel,ExporterConstants.SLING_MODEL_EXPORTER_NAME,String.class,options);
} catch (ExportException e) {
e.printStackTrace();
} catch (MissingExporterException e) {
e.printStackTrace();
}
return s;
}
}
The solution to this query -
@JsonIgnore
public String getJsonString() {
ObjectMapper mapper = new ObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
JsonNode node = mapper.valueToTree(this);
String jsonString =null;
try {
jsonString = mapper.writeValueAsString(node);
} catch (JsonProcessingException e) {
e.getMessage();
}
return jsonString;
}


