Apache Camel Rest 自定义 Json 反序列化器

Apache Camel Rest Custom Json Deserializer

我将 Camel 2.16.0 用于 Camel Rest 项目。我已经引入了一个抽象类型,我需要一个自定义反序列化器来处理它。这在我的反序列化单元测试中按预期工作,我将自定义反序列化器注册到 Objectmapper 以进行测试。据我了解,也可以将自定义模块注册到 Camel 使用的 Jackson Objectmapper (camel json)。

我的配置:

...
<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>

我的模块:

package myPackage;

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

public class MyModule extends SimpleModule {

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

}

Camel 休息配置:

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

当 运行 服务并调用一个使用 objectmapper 的函数时,我得到异常:

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

对我的设置有什么问题有什么建议吗?

我找到了 this 问题的解决方案并将此实现用于我的自定义 jackson 数据格式:

public class JacksonDataFormatExtension extends JacksonDataFormat {

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

  protected void doStart() throws Exception {
    addModule(new MyModule());
    super.doStart();
  }
}