You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Claus Ibsen (JIRA)" <ji...@apache.org> on 2017/11/01 09:23:00 UTC
[jira] [Updated] (CAMEL-11970) JacksonDataFormat does not pickup
custom ObjectMapper from Registry
[ https://issues.apache.org/jira/browse/CAMEL-11970?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Claus Ibsen updated CAMEL-11970:
--------------------------------
Issue Type: Improvement (was: Bug)
> JacksonDataFormat does not pickup custom ObjectMapper from Registry
> -------------------------------------------------------------------
>
> Key: CAMEL-11970
> URL: https://issues.apache.org/jira/browse/CAMEL-11970
> Project: Camel
> Issue Type: Improvement
> Components: camel-jackson
> Affects Versions: 2.19.2, 2.19.3, 2.20.0
> Environment: Spring Boot 1.5.7.RELEASE
> org.apache.camel:*2.19.2
> A SpringRouteBuilder bean:
> {code:java}
> @Component
> public class CustomerCreatedRoutes extends SpringRouteBuilder
> {
> @Value("${spring.application.name}")
> private String groupId;
> private final Helper helper;
> @Autowired
> public CustomerCreatedRoutes(Helper helperParm)
> {
> helper = helperParm;
> }
> @Override
> public void configure() throws Exception
> {
> KafkaTopics.DomainEvent eventType = KafkaTopics.DomainEvent.CUSTOMER_CREATED;
> from(eventType.buildConsumerUri(groupId)) //
> .routeId(eventType.getDefaultRouteId()) //
> .unmarshal() //
> .json(JsonLibrary.Jackson, eventType.getPayloadClass()) //
> .id(eventType.getDefaultUnmarshalId()) //
> .bean(helper, "createDefaultPolicy") //
> .end();
> }
> }
> {code}
> Reporter: Tim Lark
> Priority: Major
> Fix For: 2.19.4, 2.20.1
>
>
> When a custom ObjectMapper is properly configured as a Spring bean and exists in the Registry, it is ignored when the {{JacksonDataFormat.doStart}} method is invoked.
> The beginning of this method does a null check on {{objectMapper}} and simply creates one via {{new ObjectMapper()}} if null.
> I've prototyped a more robust solution below, which does pickup our custom ObjectMapper bean:
> +Before:+
> {code:java}
> @Override
> protected void doStart() throws Exception {
> if (objectMapper == null) {
> objectMapper = new ObjectMapper();
> }
> ...
> {code}
> +After:+
> {code:java}
> @Override
> protected void doStart() throws Exception {
> if (objectMapper == null) {
> CamelContext context = getCamelContext();
> if (context == null) {
> LOG.error("doStart: No camelContext defined");
> }
> else {
> Map<String, ObjectMapper> mappersByName = context
> .getRegistry()
> .findByTypeWithName(ObjectMapper.class);
> LOG.debug("doStart: Found objectMappers={}", mappersByName);
> if (mappersByName.size() >= 1) {
> Map.Entry<String, ObjectMapper> mapperByName = mappersByName
> .entrySet()
> .iterator()
> .next();
> objectMapper = mapperByName.getValue();
> LOG.debug("doStart: Using objectMapper=[name:{}, {}]", mapperByName.getKey(), objectMapper);
> }
> }
> if (objectMapper == null) {
> objectMapper = new ObjectMapper();
> LOG.warn("doStart: Using new default objectMapper={}", objectMapper);
> }
> }
> ...
> {code}
> An enhancement to this would be to allow the *bean name* to be specified instead of simply choosing the first one found.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)