Xml到Json Custom Transformer没有响应

时间:2014-03-10 14:41:27

标签: mule esb

我正在尝试按照以下流程将xml响应转换为JSON

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.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/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <data-mapper:config name="json_to_xml" transformationGraphPath="json_to_xml.grf" doc:name="json_to_xml"/>
    <custom-transformer class="org.hhmi.transformer.XmlToJson" name="XmlToJson" doc:name="Java" mimeType="application/json"/>
    <flow name="sumtotalFlow1" doc:name="sumtotalFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9119" doc:name="HTTP"/>
        <logger level="INFO" doc:name="Logger"/>
        <data-mapper:transform config-ref="json_to_xml" doc:name="JSON To XML"/>
        <logger level="DEBUG" doc:name="Logger" message="#[message.payload]"/>
        <https:outbound-endpoint exchange-pattern="request-response" host="stage.sumtotalsystems.com" port="443" path="webservice/services/WFM3Service" method="POST" doc:name="HTTP" contentType="text/xml"/>
        <logger message="SOAP Resonse from SumTotal #[message.payload]" level="DEBUG" doc:name="Logger"/>
        <mulexml:xslt-transformer mimeType="text/xml" maxIdleTransformers="2" maxActiveTransformers="5" xsl-file="XmlResponse.xsl" doc:name="XSLT"/>
        <object-to-string-transformer mimeType="text/xml" doc:name="Object to String"/>
     <transformer ref="XmlToJson" doc:name="XmlToJson"/>
    </flow>
</mule>

流程一直工作到Object to String Transformer。下面是Custom Transformer for XmlToJson转换。它似乎挂在xmlSearlizer.read(xml)行。

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;

import org.apache.log4j.Logger;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

public class XmlToJson extends AbstractTransformer {
    public static Logger logger = Logger.getLogger(XmlToJson.class);

    @Override
    protected Object doTransform(Object src, String enc)
            throws TransformerException {

        // String xml = (String) src;
        logger.debug("XmlToJson Payload " + src.toString());
        System.out.println("XmlToJson Payload " + src.toString());
        XMLSerializer xmlSerializer = new XMLSerializer();
        xmlSerializer.setSkipNamespaces(true);
        xmlSerializer.setTrimSpaces(true);
        xmlSerializer.setRemoveNamespacePrefixFromElements(true);
        JSON json = xmlSerializer.read(src.toString());
        System.out.println("XmlToJson json.toString() " + json.toString());
        return json.toString();
    }
}

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

如果您使用XMLSerializer中的net.sf.json-lib,则必须提供可选 XOM依赖关系,否则执行将失败。

使用:

    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <classifier>jdk15</classifier>
    </dependency>
    <dependency>
        <groupId>xom</groupId>
        <artifactId>xom</artifactId>
        <version>1.1</version>
    </dependency>