64.4. 使用 Jackson 进行自动类型转换
camel-jackson
模块允许将 Jackson 集成为 Type Converter。这的工作方式类似于 JAXB,它们与 Camel 类型的转换器集成。
要使用这个 camel-jackson
必须启用,这可以通过在 CamelContext
全局选项上设置以下选项来实现,如下所示:
@Bean CamelContextConfiguration contextConfiguration() { return new CamelContextConfiguration() { @Override public void beforeApplicationStart(CamelContext context) { // Enable Jackson JSON type converter. context.getGlobalOptions().put(JacksonConstants.ENABLE_TYPE_CONVERTER, "true"); // Allow Jackson JSON to convert to pojo types also // (by default Jackson only converts to String and other simple types) getContext().getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_TO_POJO, "true"); } @Override public void afterApplicationStart(CamelContext camelContext) { } }; }
@Bean
CamelContextConfiguration contextConfiguration() {
return new CamelContextConfiguration() {
@Override
public void beforeApplicationStart(CamelContext context) {
// Enable Jackson JSON type converter.
context.getGlobalOptions().put(JacksonConstants.ENABLE_TYPE_CONVERTER, "true");
// Allow Jackson JSON to convert to pojo types also
// (by default Jackson only converts to String and other simple types)
getContext().getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_TO_POJO, "true");
}
@Override
public void afterApplicationStart(CamelContext camelContext) {
}
};
}
camel-jackson
类型转换器与 JAXB 集成,这意味着您可以使用 Jackson 可以使用的 JAXB
注释来注解 POJO 类。您还可以在 POJO 类中使用 Jackson 自己的注释。