Persist your data using Slice 4.3

20 May 2016
Krystian Panek

Calling all Slice users!

I'm very happy to announce that the latest version of Slice framework has just been released. Version 4.3 comes with completely new features and stability fixes.  Read all about it here.

Slice Persistence

Now you can save your data into a repository using Slice. As in Java Persistence API, Slice allows two-way data processing. Use your existing Slice models and save them back into the Sling repository.


  • Saves simple field types (primitives & wrappers) as JCR properties (for instance: String, Integer, Calendar, InputStream)
  • Supports nested models - will be persisted as subresources
  • Allows for customizing the property name with @JcrProperty
  • Supports collections and arrays of properties
  • Supports collections and arrays of nested models with Slice @Children


Consider the following model:

 class SampleModel {

    private int pageLimit;

    private PaginationType type; // PaginationType is an enum

    private String[] tags;

    private List<SubModel> subModels;

      private String notAJcrProperty; // won't be serialized

How do you save it into a repository just as it is? Use the new OSGi service, ModelPersister:

 ModelPersister modelPersister;

 Resource resource;

void updatePageLimit() {
    SampleModel model = resource.adaptTo(SampleModel.class);
    modelPersister.persist(model, resource);

Extending mapper made easy

Extending the OOTB 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. The code snippet below demonstrates how you can register your own field processor, MyCustomFieldProcessor, using multibinding:

public class CustomProcessorModule implements Module {

   protected void configure() {
      Multibinder<FieldProcessor> multibinder = Multibinder.newSetBinder(binder(), FieldProcessor.class);

@PreMapping & @PostMapping annotations

Now, if you are annotating a model with @SliceResource you can annotate one method with @PreMapping and another with @PostMapping annotation. These will be called before or after mapping is done respectively.

Also note that in exceptional cases, you can still have both annotations on a single method:

class MyComponentModel {

  private String property;

  private ModelProvider modelProvider;

  public void preMapping() {
    // modelProvider is already injected; property is not mapped yet

  public void postMapping() {
    // modelProvider and property are already set

Improvement to @Children annotation

Now, using @Children, you can annotate not only java.lang.List, but also java.lang.Set, java.lang.SortedSet or java.lang.Collection.

public class MyComponentModel {

    private Set<ChildModel> childModels;
    // ...

Bug fixing

Slice 4.3 also features fixes for some of the minor bugs. You can find a full list of them in the release notes.

What's next?

In the near future, we are planning to create an add on for JAX-RS resources. Also our existing add ons for AEM will achieve support for e.g targeting and versioning. 

Maybe you have interesting ideas for new add ons for Slice?  If so, we would like to encourage you to collaborate on Slice project on GitHub

Finally, stay tuned! Slice 5.0 is coming.

Useful links