INT-1193 Added namespace support for JSON transformers

This commit is contained in:
Mark Fisher
2010-09-14 17:47:51 -07:00
parent 46c52f1b81
commit 45d266972f
8 changed files with 549 additions and 1 deletions

View File

@@ -43,6 +43,8 @@ public class IntegrationNamespaceHandler extends AbstractIntegrationNamespaceHan
registerBeanDefinitionParser("object-to-string-transformer", new ObjectToStringTransformerParser());
registerBeanDefinitionParser("object-to-map-transformer", new ObjectToMapTransformerParser());
registerBeanDefinitionParser("map-to-object-transformer", new MapToObjectTransformerParser());
registerBeanDefinitionParser("object-to-json-transformer", new ObjectToJsonTransformerParser());
registerBeanDefinitionParser("json-to-object-transformer", new JsonToObjectTransformerParser());
registerBeanDefinitionParser("payload-serializing-transformer", new PayloadSerializingTransformerParser());
registerBeanDefinitionParser("payload-deserializing-transformer", new PayloadDeserializingTransformerParser());
registerBeanDefinitionParser("claim-check-in", new ClaimCheckInParser());

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2002-2010 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.
* 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.springframework.integration.config.xml;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author Mark Fisher
* @since 2.0
*/
public class JsonToObjectTransformerParser extends AbstractTransformerParser {
@Override
protected String getTransformerClassName() {
return "org.springframework.integration.json.JsonToObjectTransformer";
}
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String type = element.getAttribute("type");
String objectMapper = element.getAttribute("object-mapper");
builder.addConstructorArgValue(type);
if (StringUtils.hasText(objectMapper)) {
builder.addConstructorArgReference(objectMapper);
}
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2002-2010 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.
* 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.springframework.integration.config.xml;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author Mark Fisher
* @since 2.0
*/
public class ObjectToJsonTransformerParser extends AbstractTransformerParser {
@Override
protected String getTransformerClassName() {
return "org.springframework.integration.json.ObjectToJsonTransformer";
}
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String objectMapper = element.getAttribute("object-mapper");
if (StringUtils.hasText(objectMapper)) {
builder.addConstructorArgReference(objectMapper);
}
}
}

View File

@@ -1014,7 +1014,8 @@
<xsd:element name="object-to-string-transformer" type="specialized-transformer-type" />
<xsd:element name="object-to-map-transformer" type="specialized-transformer-type" />
<xsd:element name="map-to-object-transformer" type="map-to-object-transformer-type" />
<xsd:element name="object-to-json-transformer" type="object-to-json-transformer-type" />
<xsd:element name="json-to-object-transformer" type="json-to-object-transformer-type" />
<xsd:element name="chain" type="chain-type" />
</xsd:choice>
</xsd:complexType>
@@ -1553,6 +1554,81 @@
</xsd:attribute>
</xsd:complexType>
<xsd:element name="object-to-json-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that converts any Object payload to a JSON String.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="object-to-json-transformer-type">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="object-to-json-transformer-type">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
<xsd:attribute name="object-mapper" use="optional">
<xsd:annotation>
<xsd:documentation>
Reference to a Jackson ObjectMapper instance to be provided optionally
if the default ObjectMapper configuration is not desirable.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.codehaus.jackson.map.ObjectMapper" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:element name="json-to-object-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that converts a JSON String to an object.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="json-to-object-transformer-type">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="json-to-object-transformer-type">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
<xsd:attribute name="type" type="xsd:string">
<xsd:annotation>
<xsd:documentation source="java:java.lang.Class"><![CDATA[
Fully qulified name of the java type to be created by this transformer (e.g. foo.bar.Foo)
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="object-mapper" use="optional">
<xsd:annotation>
<xsd:documentation>
Reference to a Jackson ObjectMapper instance to be provided optionally
if the default ObjectMapper configuration is not desirable.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.codehaus.jackson.map.ObjectMapper" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:element name="claim-check-in" type="claimCheckTransformerType">
<xsd:annotation>
<xsd:documentation>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<json-to-object-transformer input-channel="defaultObjectMapperInput"
type="org.springframework.integration.json.JsonToObjectTransformerParserTests$TestPerson"/>
<json-to-object-transformer input-channel="customObjectMapperInput"
type="org.springframework.integration.json.JsonToObjectTransformerParserTests$TestPerson"
object-mapper="customObjectMapper"/>
<beans:bean id="customObjectMapper" class="org.springframework.integration.json.JsonToObjectTransformerParserTests$CustomObjectMapper"/>
</beans:beans>

View File

@@ -0,0 +1,166 @@
/*
* Copyright 2002-2010 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.
* 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.springframework.integration.json;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class JsonToObjectTransformerParserTests {
@Autowired
private volatile MessageChannel defaultObjectMapperInput;
@Autowired
private volatile MessageChannel customObjectMapperInput;
@Test
public void defaultObjectMapper() {
String jsonString = "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"age\":42,\"address\":{\"number\":123,\"street\":\"Main Street\"}}";
QueueChannel replyChannel = new QueueChannel();
Message<String> message = MessageBuilder.withPayload(jsonString).setReplyChannel(replyChannel).build();
this.defaultObjectMapperInput.send(message);
Message<?> reply = replyChannel.receive(0);
assertNotNull(reply);
assertNotNull(reply.getPayload());
assertEquals(TestPerson.class, reply.getPayload().getClass());
TestPerson person = (TestPerson) reply.getPayload();
assertEquals("John", person.getFirstName());
assertEquals("Doe", person.getLastName());
assertEquals(42, person.getAge());
assertEquals("123 Main Street", person.getAddress().toString());
}
@Test
public void customObjectMapper() {
String jsonString = "{firstName:'John', lastName:'Doe', age:42, address:{number:123, street:'Main Street'}}";
QueueChannel replyChannel = new QueueChannel();
Message<String> message = MessageBuilder.withPayload(jsonString).setReplyChannel(replyChannel).build();
this.customObjectMapperInput.send(message);
Message<?> reply = replyChannel.receive(0);
assertNotNull(reply);
assertNotNull(reply.getPayload());
assertEquals(TestPerson.class, reply.getPayload().getClass());
TestPerson person = (TestPerson) reply.getPayload();
assertEquals("John", person.getFirstName());
assertEquals("Doe", person.getLastName());
assertEquals(42, person.getAge());
assertEquals("123 Main Street", person.getAddress().toString());
}
static class TestPerson {
private String firstName;
private String lastName;
private int age;
private TestAddress address;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setAddress(TestAddress address) {
this.address = address;
}
public TestAddress getAddress() {
return this.address;
}
@Override
public String toString() {
return "name=" + this.firstName + " " + this.lastName
+ ", age=" + this.age + ", address=" + this.address;
}
}
static class TestAddress {
private int number;
private String street;
public void setNumber(int number) {
this.number = number;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return this.number + " " + this.street;
}
}
static class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, Boolean.TRUE);
this.configure(Feature.ALLOW_SINGLE_QUOTES, Boolean.TRUE);
}
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<object-to-json-transformer input-channel="defaultObjectMapperInput"/>
<object-to-json-transformer input-channel="customObjectMapperInput" object-mapper="customObjectMapper"/>
<beans:bean id="customObjectMapper" class="org.springframework.integration.json.ObjectToJsonTransformerParserTests$CustomObjectMapper"/>
</beans:beans>

View File

@@ -0,0 +1,181 @@
/*
* Copyright 2002-2010 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.
* 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.springframework.integration.json;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.codehaus.jackson.JsonGenerator.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ObjectToJsonTransformerParserTests {
@Autowired
private volatile MessageChannel defaultObjectMapperInput;
@Autowired
private volatile MessageChannel customObjectMapperInput;
@Test
public void defaultObjectMapper() {
TestAddress address = new TestAddress();
address.setNumber(123);
address.setStreet("Main Street");
TestPerson person = new TestPerson();
person.setFirstName("John");
person.setLastName("Doe");
person.setAge(42);
person.setAddress(address);
QueueChannel replyChannel = new QueueChannel();
Message<TestPerson> message = MessageBuilder.withPayload(person).setReplyChannel(replyChannel).build();
this.defaultObjectMapperInput.send(message);
Message<?> reply = replyChannel.receive(0);
assertNotNull(reply);
assertNotNull(reply.getPayload());
assertEquals(String.class, reply.getPayload().getClass());
String expected = "{\"address\":{\"number\":123,\"street\":\"Main Street\"},\"firstName\":\"John\",\"lastName\":\"Doe\",\"age\":42}";
assertEquals(expected, reply.getPayload());
}
@Test
public void customObjectMapper() {
TestAddress address = new TestAddress();
address.setNumber(123);
address.setStreet("Main Street");
TestPerson person = new TestPerson();
person.setFirstName("John");
person.setLastName("Doe");
person.setAge(42);
person.setAddress(address);
QueueChannel replyChannel = new QueueChannel();
Message<TestPerson> message = MessageBuilder.withPayload(person).setReplyChannel(replyChannel).build();
this.customObjectMapperInput.send(message);
Message<?> reply = replyChannel.receive(0);
assertNotNull(reply);
assertNotNull(reply.getPayload());
assertEquals(String.class, reply.getPayload().getClass());
String expected = "{address:{number:123,street:\"Main Street\"},firstName:\"John\",lastName:\"Doe\",age:42}";
assertEquals(expected, reply.getPayload());
}
static class TestPerson {
private String firstName;
private String lastName;
private int age;
private TestAddress address;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setAddress(TestAddress address) {
this.address = address;
}
public TestAddress getAddress() {
return this.address;
}
@Override
public String toString() {
return "name=" + this.firstName + " " + this.lastName
+ ", age=" + this.age + ", address=" + this.address;
}
}
static class TestAddress {
private int number;
private String street;
public int getNumber() {
return this.number;
}
public void setNumber(int number) {
this.number = number;
}
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return this.number + " " + this.street;
}
}
static class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
this.configure(Feature.QUOTE_FIELD_NAMES, Boolean.FALSE);
}
}
}