You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/11/02 12:48:22 UTC

[camel] branch master updated: CAMEL-11970: camel-jackson should use default ObjectMapper instance if there is only one in the registry.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new ab1aad5  CAMEL-11970: camel-jackson should use default ObjectMapper instance if there is only one in the registry.
ab1aad5 is described below

commit ab1aad56b62c224923070b6cdc0ab34b0b9190bb
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Nov 2 13:44:33 2017 +0100

    CAMEL-11970: camel-jackson should use default ObjectMapper instance if there is only one in the registry.
---
 .../src/main/docs/json-jackson-dataformat.adoc     | 12 +++-
 .../camel/component/jackson/JacksonDataFormat.java | 16 ++++-
 .../component/jackson/JacksonDataFormatTest.java   |  2 +-
 .../jackson/JacksonObjectMapperRegistryTest.java   | 73 ++++++++++++++++++++++
 4 files changed, 99 insertions(+), 4 deletions(-)

diff --git a/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc b/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc
index 12d32d4..cf6d114 100644
--- a/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc
+++ b/components/camel-jackson/src/main/docs/json-jackson-dataformat.adoc
@@ -12,7 +12,7 @@ from("activemq:My.Queue").
   to("mqseries:Another.Queue");
 -------------------------------
 
-### Jackson Options
+=== Jackson Options
 
 
 
@@ -45,8 +45,16 @@ The JSon Jackson dataformat supports 17 options which are listed below.
 // dataformat options: END
 
 
+=== Using custom ObjectMapper
 
-### Dependencies
+You can configure `JacksonDataFormat` to use a custom `ObjectMapper` in case you need more control of the mapping configuration.
+
+If you setup a single `ObjectMapper` in the registry, then Camel will automatic lookup and use this `ObjectMapper`.
+For example if you use Spring Boot, then Spring Boot can provide a default `ObjectMapper` for you if you have Spring MVC enabled.
+And this would allow Camel to detect that there is one bean of `ObjectMapper` class type in the Spring Boot bean registry
+and then use it. When this happens you should set a `INFO` logging from Camel.
+
+=== Dependencies
 
 To use Jackson in your camel routes you need to add the dependency
 on *camel-jackson* which implements this data format.
diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
index 31ff280..ce981a7 100644
--- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java
@@ -24,6 +24,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -439,7 +440,20 @@ public class JacksonDataFormat extends ServiceSupport implements DataFormat, Dat
     @Override
     protected void doStart() throws Exception {
         if (objectMapper == null) {
-            objectMapper = new ObjectMapper();
+            // lookup if there is a single default mapper we can use
+            if (camelContext != null) {
+                Set<ObjectMapper> set = camelContext.getRegistry().findByType(ObjectMapper.class);
+                if (set.size() == 1) {
+                    objectMapper = set.iterator().next();
+                    LOG.info("Found single ObjectMapper in Registry to use: {}", objectMapper);
+                } else if (set.size() > 1) {
+                    LOG.debug("Found {} ObjectMapper in Registry cannot use as default as there are more than one instance.", set.size());
+                }
+            }
+            if (objectMapper == null) {
+                objectMapper = new ObjectMapper();
+                LOG.debug("Creating new ObjectMapper to use: {}", objectMapper);
+            }
         }
         
         if (enableJaxbAnnotationModule) {
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonDataFormatTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonDataFormatTest.java
index 01dc521..182569a 100644
--- a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonDataFormatTest.java
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonDataFormatTest.java
@@ -28,6 +28,7 @@ import org.junit.Test;
 import static org.junit.Assert.assertEquals;
 
 public class JacksonDataFormatTest {
+
     @Test
     public void testString() throws Exception {
         testJson("\"A string\"", "A string");
@@ -41,7 +42,6 @@ public class JacksonDataFormatTest {
     @Test
     public void testList() throws Exception {
         testJson("[{\"value\":123}]", new ArrayList<>(Collections.singletonList(Collections.singletonMap("value", 123))));
-
     }
 
     private void testJson(String json, Object expected) throws Exception {
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonObjectMapperRegistryTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonObjectMapperRegistryTest.java
new file mode 100644
index 0000000..acdbd5f
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonObjectMapperRegistryTest.java
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jackson;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JacksonObjectMapperRegistryTest extends CamelTestSupport {
+
+    private JacksonDataFormat df = new JacksonDataFormat();
+    private ObjectMapper objectMapper = new ObjectMapper();
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myMapper", objectMapper);
+        return jndi;
+    }
+
+    @Test
+    public void testMarshalAndUnmarshalMap() throws Exception {
+        Map<String, Object> in = new HashMap<String, Object>();
+        in.put("name", "Camel");
+
+        MockEndpoint mock = getMockEndpoint("mock:reverse");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(Map.class);
+        mock.message(0).body().isEqualTo(in);
+
+        Object marshalled = template.requestBody("direct:in", in);
+        String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled);
+        assertEquals("{\"name\":\"Camel\"}", marshalledAsString);
+
+        template.sendBody("direct:back", marshalled);
+
+        mock.assertIsSatisfied();
+
+        assertSame(objectMapper, df.getObjectMapper());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:in").marshal(df);
+                from("direct:back").unmarshal(df).to("mock:reverse");
+            }
+        };
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].