0

I use Camel 2.16.0 for a Camel Rest project. I have introduced an abstract type that I need a custom deserializer to handle. This works as expected in my deserialization unit tests where I register my custom deserializer to the Objectmapper for the tests. To my understanding it is possible to register custom modules to the Jackson Objectmapper used by Camel as well (camel json).

My configuration:

...
<camelContext id="formsContext" xmlns="http://camel.apache.org/schema/spring">
  ...
  <dataFormats>
    <json id="json" library="Jackson" useList="true" unmarshalTypeName="myPackage.model.CustomDeserialized" moduleClassNames="myPackage.MyModule" />      
  </dataFormats>
</camelContext>

My module:

package myPackage;

import com.fasterxml.jackson.databind.module.SimpleModule;

public class MyModule extends SimpleModule {

  public MyModule() {
    super();
    addDeserializer(CustomDeserialized.class, new MyDeserializer());
  }

}

The Camel rest configuration:

restConfiguration()
.component("servlet")
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true")
.contextPath("/")
.port(8080)
.jsonDataFormat("json");

When running the service and invoking a function that utilize the objectmapper I get the exception:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of myPackage.model.CustomDeserialized, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information

Any suggestions on what is wrong with my setup?

2
  • I used an external library to add hal+json support to a test-project of mine. Basically I define my own version of DefaultDataFormatResolver and register it with the camel context. Commented Nov 23, 2018 at 14:28
  • Within the rest configuration I was able to add the custom data format then. Not sure if this is what you are asking for exactly. This also works only for an either all or nothing approach. If you only need one route to support this "feature" this is probably not the correct approach unfortunately Commented Nov 23, 2018 at 14:30

1 Answer 1

1

I found this solution to the problem and used this implementation for my custom jackson dataformat:

public class JacksonDataFormatExtension extends JacksonDataFormat {

  public JacksonDataFormatExtension() {
    super(CustomDeserialized.class);
  }

  protected void doStart() throws Exception {
    addModule(new MyModule());
    super.doStart();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't edit your solution into the original post but add it to you answer. You can self-answer question here on SO without any considerations. Please also accept your answer as soon as possible to help others in future to resolve a similar issue more easily :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.