Customize JSON Serialization of Domain-Specific POJO Models - BloomReach Experience - Open Source CMS
15-10-2019

Customize JSON Serialization of Domain-Specific POJO Models

Introduction

As explained in Model JSON Mapping Details, many built-in Jackson Mixin Types (JacksonMixInAnnotations) are included to customize the default JSON serialization behavior for the HST Content Beans.

You can also add your own Jackson Mixin Types (JacksonMixInAnnotations) in order to customize the serialization for your domain-specific POJO models. In more advanced cases, you can also configure a custom Jackson ObjectMapper bean. 

Add Custom Jackson Mixin Types

In order to add any custom Jackson Mixin Types (JacksonMixInAnnotations) in your project, add an XML file to site/components/src/main/resources/META-INF/hst-assembly/overrides/addon/org/hippoecm/hst/pagemodelapi/v09/, and configure the bean named pageModelJacksonObjectMapperExtraMixins with your custom mixin types like the following example:

  <bean id="pageModelJacksonObjectMapperExtraMixins"
        class="org.springframework.beans.factory.config.MapFactoryBean">
    <property name="sourceMap">
      <map key-type="java.lang.Class" value-type="java.lang.Class">
        <entry key="com.example.beans.OriginalBean" value="com.example.mixins.OriginalBeanMixin" />
        <!-- SNIP -->
      </map>
    </property>
  </bean>

The above example adds a pair of the original target class' FQCN (e.g, com.example.beans.OriginalBean) and the new Jackson mixin class' FQCN (com.example.mixins.OriginalBeanMixin). So, whenever serializing an OriginalBean model object, the underlying ObjectMapper will combine the annotations defined in the OriginalBeanMixin class, resulting in custom serializations without changing the original target class.

Custom Jackson ObjectMapper

If you need more advanced configuration on the underlying Jackson ObjectMapper (e.g, enabling or disabling some features), you can override the following bean definition named pageModelAggregationObjectMapper in an XML file in site/components/src/main/resources/META-INF/hst-assembly/overrides/addon/org/hippoecm/hst/pagemodelapi/v09/.

  <bean id="pageModelAggregationObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
  </bean>

 

 

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?