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



我将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());
}
}

骆驼休息配置:

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

当运行服务并调用使用对象映射器的函数时,我会得到异常:

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

关于我的设置有什么问题,有什么建议吗?

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

public class JacksonDataFormatExtension extends JacksonDataFormat {
public JacksonDataFormatExtension() {
super(CustomDeserialized.class);
}
protected void doStart() throws Exception {
addModule(new MyModule());
super.doStart();
}
}

最新更新