I am trying to serialize and deserialize POJO to JSON on Camel routes using Jackson. Some of them have Java 8 LocalDate fields, and I want them to be serialized as a YYYY-MM-DD string, and not as an array of integers.
We only use the Java configuration for our Spring Boot application, so there is no XML Camel configuration.
I have successfully created an ObjectMapper that does what I want, which is used by other parts of our system, adding this to our dependencies:
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
and this matches our application configuration:
@Bean public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { return builder .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build(); }
An example of an outbound REST route:
@Component public class MyRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { restConfiguration().component("servlet").contextPath("/mycontext") .port(8080).bindingMode(RestBindingMode.json); rest("/myendpoint) .get() .route() .to("bean:myService?method=myMethod()"); } }
An example of an incoming message route:
@Component public class MyRouteBuilder extends RouteBuilder { @Autowired private MyBean myBean; @Override public void configure() { from(uri) .unmarshal().json(JsonLibrary.Jackson) .bean(myBean); } }
However, by default, Camel creates its own instances of ObjectMapper, therefore it does not select JSR310 serializers / deserializers that are automatically added by Jackson2ObjectMapperBuilder or the WRITE_DATES_AS_TIMESTAMPS function is WRITE_DATES_AS_TIMESTAMPS . I read the Camel JSON documentation, but does not show how to add a custom DataFormat using the Spring configuration or how to apply a global setting for all types.
So, how can I say that Camel uses my ObjectMapper using only the Spring Java boot configuration?
jackson spring-boot apache-camel
David edwards
source share