Repository: camel Updated Branches: refs/heads/master 33b96b0b5 -> 2fa60111a
[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/2fa60111 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2fa60111 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2fa60111 Branch: refs/heads/master Commit: 2fa60111add917affc161570892efda4a763f56a Parents: 33b96b0 Author: Henryk Konsek <hekon...@gmail.com> Authored: Thu Mar 26 08:57:05 2015 +0100 Committer: Henryk Konsek <hekon...@gmail.com> Committed: Thu Mar 26 08:57:05 2015 +0100 ---------------------------------------------------------------------- .../converter/JacksonTypeConverters.java | 39 +++++++++++++++ .../services/org/apache/camel/TypeConverter | 17 +++++++ .../jackson/JacksonConversionsTest.java | 51 ++++++++++++++++++++ 3 files changed, 107 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2fa60111/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..edaffb2 --- /dev/null +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/converter/JacksonTypeConverters.java @@ -0,0 +1,39 @@ +/** + * 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) { + return new ObjectMapper().convertValue(value, type); + } + return null; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/2fa60111/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/2fa60111/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); + } + }; + } + +}