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 2015/03/31 11:45:42 UTC
[1/2] camel git commit: [CAMEL-8554] Jackons component now provides
map => pojo fallback converter.
Repository: camel
Updated Branches:
refs/heads/master 780b41b47 -> 116e4026f
[CAMEL-8554] Jackons component now provides map => pojo fallback converter.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/116e4026
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/116e4026
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/116e4026
Branch: refs/heads/master
Commit: 116e4026fb3d284eaadba26bddf5ebd02a658b65
Parents: f678aa4
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Mar 31 11:32:55 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Mar 31 11:48:04 2015 +0200
----------------------------------------------------------------------
.../jackson/converter/JacksonTypeConverters.java | 13 ++++++++-----
.../component/jackson/JacksonConversionsTest.java | 2 --
.../camel/component/jetty/DefaultJettyHttpBinding.java | 2 +-
3 files changed, 9 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/116e4026/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
index 6db03a1..efe51ce 100644
--- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
@@ -30,12 +30,15 @@ public final class JacksonTypeConverters {
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
+ if (type.isAssignableFrom(String.class)) {
+ // do not convert to String
+ return null;
+ }
+
if (value instanceof Map) {
- try {
- return new ObjectMapper().convertValue(value, type);
- } catch (Exception ex) {
- // Just catch the exception and return null when the ObjectMapper cannot convert the value
- return null;
+ ObjectMapper mapper = new ObjectMapper();
+ if (mapper.canSerialize(type)) {
+ return mapper.convertValue(value, type);
}
}
// Just return null to let other fallback converter to do the job
http://git-wip-us.apache.org/repos/asf/camel/blob/116e4026/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java
index 20427b5..0daf1cc 100644
--- a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java
@@ -20,12 +20,10 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class JacksonConversionsTest extends CamelTestSupport {
- protected MockEndpoint results;
@Test
public void shouldConvertMapToPojo() {
http://git-wip-us.apache.org/repos/asf/camel/blob/116e4026/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
index 7b73a8a..8c55d3e 100644
--- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
+++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
@@ -44,8 +44,8 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding {
private HeaderFilterStrategy headerFilterStrategy = new HttpHeaderFilterStrategy();
private boolean throwExceptionOnFailure;
private boolean transferException;
+
public DefaultJettyHttpBinding() {
-
}
public void populateResponse(Exchange exchange, JettyContentExchange httpExchange) throws Exception {
[2/2] camel git commit: Revert "Revert "[CAMEL-8554] Jackons
component now provides map => pojo fallback converter.""
Posted by da...@apache.org.
Revert "Revert "[CAMEL-8554] Jackons component now provides map => pojo fallback converter.""
This reverts commit 60ef47d6e8086f3d0b1edc03ceb29b2c5fb24d15.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f678aa44
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f678aa44
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f678aa44
Branch: refs/heads/master
Commit: f678aa4480dbb7ad2bdcaa77dda8ed12feb35b27
Parents: 780b41b
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Mar 31 10:59:35 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Mar 31 11:48:04 2015 +0200
----------------------------------------------------------------------
.../converter/JacksonTypeConverters.java | 45 +++++++++++++++++
.../services/org/apache/camel/TypeConverter | 17 +++++++
.../jackson/JacksonConversionsTest.java | 51 ++++++++++++++++++++
3 files changed, 113 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/f678aa44/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
new file mode 100644
index 0000000..6db03a1
--- /dev/null
+++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java
@@ -0,0 +1,45 @@
+/**
+ * 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.converter;
+
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.camel.Exchange;
+import org.apache.camel.FallbackConverter;
+import org.apache.camel.spi.TypeConverterRegistry;
+
+public final class JacksonTypeConverters {
+
+ private JacksonTypeConverters() {
+ }
+
+ @FallbackConverter
+ public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
+ if (value instanceof Map) {
+ try {
+ return new ObjectMapper().convertValue(value, type);
+ } catch (Exception ex) {
+ // Just catch the exception and return null when the ObjectMapper cannot convert the value
+ return null;
+ }
+ }
+ // Just return null to let other fallback converter to do the job
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/f678aa44/components/camel-jackson/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/main/resources/META-INF/services/org/apache/camel/TypeConverter b/components/camel-jackson/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
new file mode 100644
index 0000000..6ad0528
--- /dev/null
+++ b/components/camel-jackson/src/main/resources/META-INF/services/org/apache/camel/TypeConverter
@@ -0,0 +1,17 @@
+#
+# 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.
+#
+org.apache.camel.component.jackson.converter.JacksonTypeConverters
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/f678aa44/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java
new file mode 100644
index 0000000..20427b5
--- /dev/null
+++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonConversionsTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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 org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JacksonConversionsTest extends CamelTestSupport {
+ protected MockEndpoint results;
+
+ @Test
+ public void shouldConvertMapToPojo() {
+ String name = "someName";
+ Map<String, String> pojoAsMap = new HashMap<String, String>();
+ pojoAsMap.put("name", name);
+
+ TestPojo testPojo = (TestPojo) template.requestBody("direct:test", pojoAsMap);
+
+ assertEquals(name, testPojo.getName());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:test").convertBodyTo(TestPojo.class);
+ }
+ };
+ }
+
+}