简体   繁体   中英

Accessing Payload using JAVA (MULE)

I'm currently working on a flow using MULE and I need to get some information using a java class. To do some validations, transformations and then call a stored procedure in an Oracle database, I need to get the information stored in <type:Data> , this is the flow:

<flow name="INSERT">
    <http:listener config-ref="CONFIG_1" path="/afis/afisInsert" allowedMethods="POST" doc:name="HTTP"/>
    <cxf:proxy-service port="InsertDATA" namespace="http://www.morpho.com/SIIMInterface/" service="InsertService" payload="envelope" wsdlLocation="AFISINSERT.wsdl" doc:name="CXF" configuration-ref="CXF_Configuration"/>
    <component class="Transform" doc:name="Java"/>  
    <logger message="String message : #[payload]" level="INFO" doc:name="Logger"/>      
</flow>

I'm sending a POST request with the following structure:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:type="http://XXX/ZZ/WWW/YYY/">
<soapenv:Header/>
<soapenv:Body>
<input>
    <type:Data>
        <type:id>1</type:id>
        <type:sampleType>?</type:sampleType>
            <!--Optional:-->
            <type:tenprint>
                <type:width>600</type:width>
                <type:height>800</type:height>
                <type:format>S</type:format>
            </type:tenprint>
    </type:Data>
    <type:PersonID>123</type:PersonID>
    <type:Priority>3</type:Priority>
</input>
<soapenv:Body>
</soapenv:Envelope>

My java class here:

public class CLASE extends AbstractMessageTransformer{

public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {

String mensajeString = new String();    
     try {
        return message.getPayloadAsString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

By using Mule variables, I'm able to access to the nodes of the xml using XPATH, in the log, the xml request is correctly printed. I have tried so many things, but I cannot access to the payload information correctly, How can I get the information stored into the Payload using JAVA ?

You can choose one of two options, to get the information stored in <type:Data> from Java code.

  1. Parse the POST data into an XML document inside the Java code:

     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { BufferInputStream payload = (BufferInputStream) message.getPayload(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(payload); String typeData = doc.getDocumentElement().getElementsByTagName("type:Data").item(0).getTextContent();
  2. Use XPATH to extract the required data, store it in a variable, and then proceed in the Java code:

     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { String typeData = message.getInvocationProperty("variableName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM