Slice persistence is a module that allows you to save Slice models back into Sling repository.
String
, int
, Calendar
, etc.)@JcrProperty
@Children
Following code snippet shows how to use Slice Persistence.
Consider following model:
@SliceResource public class SampleModel { @JcrProperty private int pageLimit; @JcrProperty private PaginationType type; // PaginationType is an enum @JcrProperty private String[] tags; @JcrProperty @Children(SubModel.class) private List<SubModel> subModels; private String notAJcrProperty; // won't be serialized //... } |
Model can be persisted as follows:
@Inject private ModelPersister modelPersister; //... SampleModel model = resource.adaptTo(SampleModel.class); // read model from repository model.setPageLimit(123); modelPersister.persist(model, resource); resource.getResourceResolver().commit(); |
Read more about Slice Persistence
Extending the out-of-the-box mapper is now much easier. Thanks to Guice multibindings you can easily register your own Field Processor which can map a field in a custom way. Below code snippet demonstrate how you can register your own field processor ({{MyCustomFieldProcessor}}) with use of multibinding:
public class CustomProcessorModule implements Module { @Override protected void configure() { Multibinder<FieldProcessor> multibinder = Multibinder.newSetBinder(binder(), FieldProcessor.class); multibinder.addBinding().to(MyCustomFieldProcessor.class); } } |
Read more about extending mapper.
Now, having a model annotated with @SliceResource
you can annotate method with @PreMapping
or @PostMapping
annotation. Such method will be called before or after mapping is done.
Example usage:
@SliceResource public class MyComponentModel { @JcrProperty private String property; @Inject private ModelProvider modelProvider; @PreMapping public void preMapping() { // modelProvider is already injected; property is not mapped yet } @PostMapping public void postMapping() { // modelProvider and property are already set } } |
Now using @Children you can annotate not only java.lang.List
, but also java.lang.Set
, java.lang.SortedSet
or java.lang.Collection
.
@SliceResource public class MyComponentModel { @Chldren(ChildModel.class) @JcrProperty private Set<ChildModel> childModels; ... } |
Slice 4.3.0 brings also fixes for some minor bugs. You can find full list of them in release notes.