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 <davscl...@apache.org> Authored: Tue Mar 31 10:59:35 2015 +0200 Committer: Claus Ibsen <davscl...@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); + } + }; + } + +}