在许多情况下,可能需要把类转换为json,或者将json转换为xml,xlst等各种格式.这里简单讲解json和xml,xlst等之间的转换过程.
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:mxml="http://www.mulesoft.org/schema/mule/xml" xmlns:spring="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd"> <json:is-json-filter name="jsonFilter" validateParsing="true"/> <json:mapper name="myMapper"> <json:mixin mixinClass="com.easyway.esb.mule.json.transformers.FruitCollectionMixin" targetClass="com.easyway.esb.mule.json.transformers.FruitCollection"/> <json:mixin mixinClass="com.easyway.esb.mule.json.transformers.AppleMixin" targetClass="com.easyway.esb.mule.json.model.Apple"/> </json:mapper> <json:json-to-object-transformer name="jsonToFruitCollection" returnClass="com.easyway.esb.mule.json.transformers.FruitCollection" mapper-ref="myMapper"> <!-- test augmenting the mixin map with local mixins --> <json:deserialization-mixin mixinClass="com.easyway.esb.mule.json.transformers.OrangeMixin" targetClass="com.easyway.esb.mule.json.model.Orange"/> </json:json-to-object-transformer> <json:object-to-json-transformer name="fruitCollectionToJson" sourceClass="com.easyway.esb.mule.json.transformers.FruitCollection"> <json:serialization-mixin mixinClass="com.easyway.esb.mule.json.transformers.FruitCollectionMixin" targetClass="com.easyway.esb.mule.json.transformers.FruitCollection"/> <json:serialization-mixin mixinClass="com.easyway.esb.mule.json.transformers.AppleMixin" targetClass="com.easyway.esb.mule.json.model.Apple"/> <json:serialization-mixin mixinClass="com.easyway.esb.mule.json.transformers.OrangeMixin" targetClass="com.easyway.esb.mule.json.model.Orange"/> </json:object-to-json-transformer> <json:json-to-xml-transformer name="jToX"/> <json:xml-to-json-transformer name="xToJ"/> <json:json-xslt-transformer name="jToJ"> <mxml:xslt-text> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> </mxml:xslt-text> </json:json-xslt-transformer> <json:json-schema-validation-filter name="jvf" schemaLocations="customer.xsd"/> </mule>
测试代码:
XmlToJson transformer = new XmlToJson(); String jsonResponse = (String) transformer.transform(XML); System.out.println("jsonResponse ="+jsonResponse); StringReader reader = new StringReader(XML); transformer = new XmlToJson(); jsonResponse = (String) transformer.transform(reader); System.out.println("jsonResponse ="+jsonResponse); byte[] bytes = XML.getBytes(); transformer = new XmlToJson(); jsonResponse = (String) transformer.transform(bytes); System.out.println("jsonResponse ="+jsonResponse); Document document = XMLUtils.toW3cDocument(XML); document.setDocumentURI("xxx"); transformer = new XmlToJson(); jsonResponse = (String) transformer.transform(document); System.out.println("jsonResponse ="+jsonResponse);
mule-jdbc-json-config.xml:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:test="http://www.mulesoft.org/schema/mule/test" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.xsd http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/current/mule-jdbc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd"> <jdbc:mysql-data-source name="MySQL_Data_Source1" url="jdbc:mysql://localhost:3306/dbtest2" user="root" password="root" transactionIsolation="UNSPECIFIED" /> <jdbc:connector name="Database" dataSource-ref="MySQL_Data_Source1" validateConnections="true" queryTimeout="-1" pollingFrequency="0" /> <flow name="httpPostTestFlow1" > <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="httpPost" ></http:inbound-endpoint> <json:json-to-object-transformer returnClass="java.util.Map"></json:json-to-object-transformer> <jdbc:outbound-endpoint exchange-pattern="one-way" queryTimeout="-1" queryKey="INSERT_TOKEN" connector-ref="Database"> <jdbc:query key="INSERT_TOKEN" value="insert into user(FirstName) values(#[message.payload.name]);"/> </jdbc:outbound-endpoint> </flow> </mule>