Expand my Community achievements bar.

Export a component to JSON with multifield elements and display contact list in JSON

Avatar

Level 1

Hello, I want to know how I can export a component to json when it has multifield elements

I have these classes but I don't really know how to display the contact list elements in the json

 

package com.program.core.models;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.ExporterOption;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

import javax.inject.Inject;
import java.util.List;


@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = "jackson", extensions = "json", options = {
    @ExporterOption(name = "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", value = "true"),
    @ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "false")
})
public class ContactCardsModel {
    
    @SlingObject
    SlingHttpServletRequest slingRequest;

    @inject
    @Via("resource")
    public List<Contact> contact;

    public List<Contact> getContact() {
        return contact;
    }
}

 

 

 

package com.program.core.models;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import javax.inject.Inject;

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Contact {

    @inject
    private String contactName;

    @inject
    private String contactEmail;

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactEmail() {
        return contactEmail;
    }

    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }
}

 

 I would really appreciate if someone could give me an idea how to do it.

3 Replies

Avatar

Employee Advisor

@LidermanGiraldoEscobar 

Can you share what response are you expecting Vs what you are getting when calling AEM Content Services:
http://localhost:4502/content/practice/us/en/test.model.tidy.json

 

here test is a page, where you might have authored your component ContactCard

Avatar

Community Advisor

Hi, 

 

You can find an example here of how to do it: https://gist.github.com/Surajkamdi/6d51779d62278b56a747e04532f39036 

Pay attention to this code:

  //Multifield Child Resource for social links
  @ChildResource(name = "socialLinks")
  Collection<SocialLinksModel> socialLinks;

 

You can check how this work here: https://medium.com/@toimrank/aem-sling-model-exporter-90dc872835b9 

 

Hope this helps.

 



Esteban Bustamante

Avatar

Administrator

@LidermanGiraldoEscobar Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni