Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-11970

JacksonDataFormat does not pickup custom ObjectMapper from Registry

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.19.2, 2.19.3, 2.20.0
    • 2.21.0
    • camel-jackson
    • None
    • Unknown
    • Patch

    Description

      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:

          @Override
          protected void doStart() throws Exception {
              if (objectMapper == null) {
                  objectMapper = new ObjectMapper();
              }
      ...
      

      After:

          @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);
                  }
              }
      ...
      

      An enhancement to this would be to allow the bean name to be specified instead of simply choosing the first one found.

      Attachments

        Activity

          People

            davsclaus Claus Ibsen
            tlark Tim Lark
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: