3

I'm using DD4T Java release v2.0.4. FasterXML Jackson package v2.6.4.

When deserializing a Page JSON I'm getting the following error:

com.fasterxml.jackson.databind.JsonMappingException:
Can not construct instance of org.dd4t.contentmodel.impl.BaseField,
problem: abstract types either need to be mapped to concrete types,
have custom deserializer, or be instantiated with additional type
information at [Source: {"RevisionDate":"2016-05-06T14:15:45",
"Filename":"article","LastPublishedDate":"0001-01-01T00:00:00",
"PageTemplate":{"FileExtension":"html","RevisionDate":"2016-05-06T14:04:10.727",
"MetadataFields":{"view":{"Name":"view","Values":["Jump"],"NumericValues":
...................etc etc etc .......... at line: 1, column: 200]
(through reference chain: org.dd4t.contentmodel.impl.PageImpl["PageTemplate"]->
org.dd4t.contentmodel.impl.PageTemplateImpl["MetadataFields"]->
java.util.LinkedHashMap["view"])

The error makes sense, since the MetadataFields declaration in BaseRepositoryLocalItem looks like:

@JsonProperty ("MetadataFields")
@JsonDeserialize (contentAs = BaseField.class)
protected Map<String, Field> metadata;

How is this supposed to work? Should I create my own specific model object? Should I create a custom deserializer? Or is it actually a bug?

Thanks in advance!

2

Self answering ;)

I was missing a configuration in the applicationContext.xml (or dispatcher-servlet.xml if you're using Spring MVC):

<context:annotation-config/> (or <context:component-scan/>)

This enables Spring framework to execute annotations such as @PostConstruct. Once this was in place, method Init from JsonDataBinder would be executed:

@PostConstruct
@Override
protected void init () {...}

As such, the Json mapper would be initialized and configured correctly.

My bean definitions look like this now:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config/>

...

<bean id="dataBindFactory" scope="singleton" class="org.dd4t.databind.DataBindFactory" factory-method="getInstance">
    <property name="dataBinder" ref="dataBinder"/>
</bean>

<bean id="dataBinder" class="org.dd4t.databind.builder.json.JsonDataBinder" factory-method="getInstance">
    <property name="viewModelPackageRoot" value="mydd4t.models"/>
    <property name="viewModelMetaKeyName" value="view"/>
    <property name="renderDefaultComponentModelsOnly" value="false"/>
    <property name="renderDefaultComponentsIfNoModelFound" value="true"/>
    <property name="converter" ref="modelConverter"/>
    <property name="concreteComponentPresentationImpl"
              value="org.dd4t.contentmodel.impl.ComponentPresentationImpl"/>
    <property name="concreteComponentTemplateImpl" value="org.dd4t.contentmodel.impl.ComponentTemplateImpl"/>
    <property name="concreteComponentImpl" value="org.dd4t.contentmodel.impl.ComponentImpl"/>
    <property name="concreteFieldImpl" value="org.dd4t.contentmodel.impl.BaseField"/>
</bean>

<bean id="modelConverter" class="org.dd4t.databind.builder.json.JsonModelConverter"/>
</beans>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.