Saturday, July 30, 2016

Sling Model, the easier way to map data model in AEM

In the previous post, we see  how to adapt the JCR data to POJO model using Sling Adaptable interface. There are easier way to map jcr resource to domain models. Here comes the Sling Model for our help from AEM 6.0 onwards.

In the below example i'm trying to illustrate with same VehicleData model using Sling Model.

Only thing we need to annotate the VehicleData model class as follows.

import javax.inject.Inject;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;

@Model(adaptables=Resource.class)
public class VehicleData {

@Inject
private String vin;
@Inject
private String manufacturer;

/**
* @return the vin
*/
public String getVin() {
return vin;
}

/**
* @param vin the vin to set
*/
public void setVin(String vin) {
this.vin = vin;
}

/**
* @return the manufacturer
*/
public String getManufacturer() {
return manufacturer;
}

/**
* @param manufacturer the manufacturer to set
*/
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

}

ie all, now we easier to adapt the resource to vehicledata domain model.


VehicleData vehicleData = resource.adaptTo(VehicleData.class);

No comments:

Post a Comment