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

JacksonDataFormat does not pickup custom ObjectMapper from Registry

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.19.2, 2.19.3, 2.20.0
    • Fix Version/s: 2.21.0
    • Component/s: camel-jackson
    • Labels:
      None
    • Estimated Complexity:
      Unknown
    • Flags:
      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

            • Assignee:
              davsclaus Claus Ibsen
              Reporter:
              tlark Tim Lark
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: