INT-3193: Add ObjectToMapTransformer flatten attr

JIRA: https://jira.springsource.org/browse/INT-3193
This commit is contained in:
Mauro Franceschini
2013-11-04 10:45:22 +02:00
committed by Artem Bilan
parent 4dd95c41ea
commit cd3f13ac91
5 changed files with 71 additions and 1 deletions

View File

@@ -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");
}
}

View File

@@ -2169,6 +2169,15 @@
<xsd:complexContent>
<xsd:extension base="specialized-transformer-type">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
<xsd:attribute name="flatten" type="xsd:string" default="true">
<xsd:annotation>
<xsd:documentation>
Specifies if the result Map of Maps should be transformed further to flat keys of
object's property paths.
Default is 'true'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

View File

@@ -15,4 +15,12 @@
<object-to-map-transformer input-channel="directInput" output-channel="output"/>
<channel id="nestedInput"/>
<channel id="nestedOutput">
<queue capacity="1"/>
</channel>
<object-to-map-transformer input-channel="nestedInput" output-channel="nestedOutput" flatten="false"/>
</beans:beans>

View File

@@ -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<Employee> message = MessageBuilder.withPayload(employee).build();
nestedInput.send(message);
@SuppressWarnings("unchecked")
Message<Map<String, Object>> outputMessage = (Message<Map<String, Object>>) nestedOutput.receive(1000);
Map<String, Object> 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();

View File

@@ -184,10 +184,33 @@ public class Kid {
   // setters and getters are omitted
}]]></programlisting>
</para>
<para>
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.
</para>
<para>
For example:
<programlisting language="java"><![CDATA[public class Parent {
private Child child;
private String name;
// setters and getters are omitted
}
public class Child {
private String name;
private List<String> nickNames;
// setters and getters are omitted
}]]></programlisting>
... will be transformed to a Map which looks like this:
<code>{name=George, child={name=Jenna, nickNames=[Bimbo, ...]}}</code>
</para>
<para>
To configure these transformers, Spring Integration provides namespace support
Object-to-Map:
<programlisting language="xml"><![CDATA[<int:object-to-map-transformer input-channel="directInput" output-channel="output"/>]]></programlisting>
or
<programlisting language="xml"><![CDATA[<int:object-to-map-transformer input-channel="directInput" output-channel="output" flatten="false"/>]]></programlisting>
Map-to-Object
<programlisting language="xml"><![CDATA[<int:map-to-object-transformer input-channel="input" 
                       output-channel="output"