CAMEL-6446: camel-jackson support using JAXB annotations. Thanks to Chris Geer for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/274d018d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/274d018d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/274d018d Branch: refs/heads/camel-2.11.x Commit: 274d018daf0abe117f31d8858dd5ead31991a6ed Parents: bb2afbb Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Jun 25 11:05:14 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Jun 25 11:06:06 2013 +0200 ---------------------------------------------------------------------- components/camel-jackson/pom.xml | 5 ++ .../component/jackson/JacksonDataFormat.java | 13 +++- .../jackson/JacksonJAXBAnnotationTest.java | 65 ++++++++++++++++++++ .../camel/component/jackson/TestJAXBPojo.java | 50 +++++++++++++++ .../features/src/main/resources/features.xml | 1 + 5 files changed, 131 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/274d018d/components/camel-jackson/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-jackson/pom.xml b/components/camel-jackson/pom.xml index d15f8d2..85c40d5 100644 --- a/components/camel-jackson/pom.xml +++ b/components/camel-jackson/pom.xml @@ -45,6 +45,11 @@ <artifactId>jackson-databind</artifactId> <version>${jackson2-version}</version> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.module</groupId> + <artifactId>jackson-module-jaxb-annotations</artifactId> + <version>${jackson2-version}</version> + </dependency> <!-- testing --> <dependency> http://git-wip-us.apache.org/repos/asf/camel/blob/274d018d/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/JacksonDataFormat.java ---------------------------------------------------------------------- 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 2b8246f..231adf7 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 @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; import org.apache.camel.Exchange; import org.apache.camel.spi.DataFormat; @@ -41,7 +42,7 @@ public class JacksonDataFormat implements DataFormat { * Use the default Jackson {@link ObjectMapper} and {@link Map} */ public JacksonDataFormat() { - this(new ObjectMapper(), HashMap.class); + this(HashMap.class); } /** @@ -51,7 +52,7 @@ public class JacksonDataFormat implements DataFormat { * @param unmarshalType the custom unmarshal type */ public JacksonDataFormat(Class<?> unmarshalType) { - this(new ObjectMapper(), unmarshalType); + this(unmarshalType, null); } /** @@ -63,7 +64,13 @@ public class JacksonDataFormat implements DataFormat { * See also http://wiki.fasterxml.com/JacksonJsonViews */ public JacksonDataFormat(Class<?> unmarshalType, Class<?> jsonView) { - this(new ObjectMapper(), unmarshalType, jsonView); + this.objectMapper = new ObjectMapper(); + this.unmarshalType = unmarshalType; + this.jsonView = jsonView; + + // Enables JAXB processing + JaxbAnnotationModule module = new JaxbAnnotationModule(); + this.objectMapper.registerModule(module); } /** http://git-wip-us.apache.org/repos/asf/camel/blob/274d018d/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java new file mode 100644 index 0000000..91afad8 --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/JacksonJAXBAnnotationTest.java @@ -0,0 +1,65 @@ +/** + * 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 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 JacksonJAXBAnnotationTest extends CamelTestSupport { + + @Test + public void testMarshalJAXBObject() throws Exception { + TestJAXBPojo in = new TestJAXBPojo(); + in.setName("Camel"); + + MockEndpoint mock = getMockEndpoint("mock:reversePojo"); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(TestJAXBPojo.class); + mock.message(0).body().equals(in); + + Object marshalled = template.requestBody("direct:inPojo", in); + String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled); + assertEquals("{\"PojoName\":\"Camel\"}", marshalledAsString); + + template.sendBody("direct:backPojo", marshalled); + + mock.assertIsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + + JacksonDataFormat format = new JacksonDataFormat(); + + from("direct:in").marshal(format); + from("direct:back").unmarshal(format).to("mock:reverse"); + + JacksonDataFormat formatPojo = new JacksonDataFormat(TestJAXBPojo.class); + + from("direct:inPojo").marshal(formatPojo); + from("direct:backPojo").unmarshal(formatPojo).to("mock:reversePojo"); + + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/274d018d/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java ---------------------------------------------------------------------- diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java new file mode 100644 index 0000000..20cee37 --- /dev/null +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/TestJAXBPojo.java @@ -0,0 +1,50 @@ +/** + * 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 javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "XMLPojo") +public class TestJAXBPojo { + + @XmlElement(name = "PojoName") + private String name; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object obj) { + return this.name.equals(((TestPojo) obj).getName()); + } + + @Override + public int hashCode() { + return name != null ? name.hashCode() : 0; + } + + @Override + public String toString() { + return "TestJAXBPojo[" + name + "]"; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/274d018d/platforms/karaf/features/src/main/resources/features.xml ---------------------------------------------------------------------- diff --git a/platforms/karaf/features/src/main/resources/features.xml b/platforms/karaf/features/src/main/resources/features.xml index aa4ad1b..9d5a7b9 100644 --- a/platforms/karaf/features/src/main/resources/features.xml +++ b/platforms/karaf/features/src/main/resources/features.xml @@ -458,6 +458,7 @@ <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-core/${jackson2-version}</bundle> <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-databind/${jackson2-version}</bundle> <bundle dependency='true'>mvn:com.fasterxml.jackson.core/jackson-annotations/${jackson2-version}</bundle> + <bundle dependency='true'>mvn:com.fasterxml.jackson.module/jackson-module-jaxb-annotations/${jackson2-version}</bundle> <feature version='${project.version}'>camel-core</feature> <bundle>mvn:org.apache.camel/camel-jackson/${project.version}</bundle> </feature>