From cd3f13ac91f433ff3d899b3807bb095c5a6a2eee Mon Sep 17 00:00:00 2001 From: Mauro Franceschini Date: Mon, 4 Nov 2013 10:45:22 +0200 Subject: [PATCH] INT-3193: Add ObjectToMapTransformer flatten attr JIRA: https://jira.springsource.org/browse/INT-3193 --- .../xml/ObjectToMapTransformerParser.java | 4 ++- .../config/xml/spring-integration-3.0.xsd | 9 ++++++ ...ectToMapTransformerParserTests-context.xml | 8 ++++++ .../ObjectToMapTransformerParserTests.java | 28 +++++++++++++++++++ src/reference/docbook/transformer.xml | 23 +++++++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/ObjectToMapTransformerParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/ObjectToMapTransformerParser.java index 6226f63cfa..501ffaf4d4 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/ObjectToMapTransformerParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/ObjectToMapTransformerParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import org.w3c.dom.Element; /** * @author Oleg Zhurakousky + * @author Mauro Franceschini * @since 2.0 */ public class ObjectToMapTransformerParser extends AbstractTransformerParser { @@ -33,5 +34,6 @@ public class ObjectToMapTransformerParser extends AbstractTransformerParser { @Override protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "flatten", "shouldFlattenKeys"); } } diff --git a/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-3.0.xsd b/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-3.0.xsd index 4c6867c967..a7b47c6edb 100644 --- a/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-3.0.xsd +++ b/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-3.0.xsd @@ -2169,6 +2169,15 @@ + + + + Specifies if the result Map of Maps should be transformed further to flat keys of + object's property paths. + Default is 'true'. + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests-context.xml index c0dfd12e5d..11ef5d780d 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests-context.xml @@ -15,4 +15,12 @@ + + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests.java index 057ecb086b..75eb4ecd6f 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/ObjectToMapTransformerParserTests.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.hamcrest.Matchers; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -40,10 +41,12 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; /** * @author Oleg Zhurakousky * @author Gunnar Hillert + * @author Mauro Franceschini */ @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) @@ -57,6 +60,14 @@ public class ObjectToMapTransformerParserTests { @Qualifier("output") private PollableChannel output; + @Autowired + @Qualifier("nestedInput") + private MessageChannel nestedInput; + + @Autowired + @Qualifier("nestedOutput") + private PollableChannel nestedOutput; + @SuppressWarnings("unchecked") @Test @@ -90,6 +101,23 @@ public class ObjectToMapTransformerParserTests { directInput.send(message); } + @Test + public void testObjectToNotFlattenedMapTransformer(){ + Employee employee = this.buildEmployee(); + + Message message = MessageBuilder.withPayload(employee).build(); + nestedInput.send(message); + + @SuppressWarnings("unchecked") + Message> outputMessage = (Message>) nestedOutput.receive(1000); + Map transformedMap = outputMessage.getPayload(); + assertNotNull(outputMessage.getPayload()); + + assertEquals(employee.getCompanyName(), transformedMap.get("companyName")); + assertThat(transformedMap.get("companyAddress"), Matchers.instanceOf(Map.class)); + assertThat(transformedMap.get("departments"), Matchers.instanceOf(List.class)); + } + @SuppressWarnings({ "unchecked", "rawtypes" }) public Employee buildEmployee(){ Address companyAddress = new Address(); diff --git a/src/reference/docbook/transformer.xml b/src/reference/docbook/transformer.xml index ee1a6d87e4..495664b267 100644 --- a/src/reference/docbook/transformer.xml +++ b/src/reference/docbook/transformer.xml @@ -184,10 +184,33 @@ public class Kid {    // setters and getters are omitted }]]> + + If you need to create a "structured" map, you can provide the 'flatten' attribute. The default value for this + attribute is 'true' meaning the default behavior; if you provide a 'false' value, then the structure will be a + map of maps. + + + For example: + nickNames; + // setters and getters are omitted +}]]> + ... will be transformed to a Map which looks like this: + {name=George, child={name=Jenna, nickNames=[Bimbo, ...]}} + To configure these transformers, Spring Integration provides namespace support Object-to-Map: ]]> + or +]]> Map-to-Object