camel-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CAMEL-11970) JacksonDataFormat does not pickup custom ObjectMapper from Registry
Date Wed, 01 Nov 2017 09:24:00 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-11970?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16233829#comment-16233829
] 

Claus Ibsen commented on CAMEL-11970:
-------------------------------------

AFAIR you can configure this with #beanId in the spring boot application.properties etc

> 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)

Mime
View raw message