简体   繁体   中英

What am I doing wrong in returning a custom object to a mule flow and How do I return an object that can be consumed by another mule flow?

Here's what I'm doing in my flow:

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

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" 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.6.0" 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">
    <spring:beans>
        <spring:bean id="JavaMule" class="com.intuit.platform.fdp.transaction.orchestration.JavaMule"/>
    </spring:beans>
    <flow name="javaflowFlow1" doc:name="javaflowFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="9090" path="java" doc:name="HTTP"/>

        <component class="com.intuit.platform.fdp.transaction.orchestration.JavaMule" doc:name="Java">

        </component>
        <set-payload value="#['This is my clientcontext'+ '13424234']" doc:name="Set Payload"/>
       <invoke object-ref="JavaMule"
        method="myMethod"
        methodArguments="#[payload]" doc:name="Invoke"/>
        <object-to-string-transformer doc:name="Object to String" returnClass="com.intuit.platform.fdp.transaction.orchestration.DummyPojo"/>
        <echo-component doc:name="Echo"/>
    </flow>
</mule>

Here's the code for myMethod .

   public DummyPojo myMethod(String payload) throws IOException {
        // do things with payload.

        DummyPojo pojo = new DummyPojo();
        pojo.setCode(0);
        pojo.setDesc(payload);
        return pojo;
    }

And the code for DummyPojo .

public class DummyPojo {

    private int code;
    private String desc;

    public int getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }


    public void setCode(int code) {
        this.code = code;
    }


    public void setDesc(String desc) {
        this.desc = desc;
    } 
}

However when I run the flow I get the following error.

The object transformed is of type: "SimpleDataType{type=java.lang.String, mimeType='*/*'}", but the expected return type is "SimpleDataType{type=com.test.DummyPojo, mimeType='text/plain'}" (org.mule.api.transformer.TransformerException). Message payload is of type: DummyPojo

Any thoughts on what Im doing wrong in my flow? I'm trying to return a custom object instead of the string.

EDIT:

I'm trying to make the object I create(DummyPojo) available to another mule flow for consumption . Ie. for example Sub flow B produces a DummyPojo as a result of its operations. How/What do I return from B such that A can consume the returned object

You are invoking the object-to-string-transformer which always returns a String but asking it to return a custom object:

<object-to-string-transformer doc:name="Object to String" returnClass="com.intuit.platform.fdp.transaction.orchestration.DummyPojo"/>

That won't work and you will see the error you are seeing. If you don't want a string, remove this transformer.

There are number of things here that are not completely correct:

You have the very same class instantiated twice: com.intuit.platform.fdp.transaction.orchestration.JavaMule.

This is called two times in a row, once using a component and once using an invoke. If JavaMule only have one method with a single parameter String signature then I would keep component and remove invoke.

The object to string transformer is incorrect just like Ryan said. Also, it is probably not what you are intending to do. In your question you mention that you want to return an object, if that is effectively what you intent, just remove the object-to-string-transformer completely. If you want to serialize the object somehow, just use a xstream's object to xml transformer or a object to bytearray transformer.

If your intention with the echo-component is to log the payload, you should use the logger element: <logger messsage="#[message.payload]" /> the echo-component is a mule 2 deprecated component that will have some undesired effects like copying the inbound properties to outbound.

EDIT :

To have a reusable flow that you can call from other flows you don't explicitly need to do nothing, you just need to call the flow with a flow-ref :

<flow-ref name="javaflowFlow1" />

This will ignore the inbound-endpoint (that anyhow you probably don't need in the flow of the question).

For more information on this topic you have the documentation , getting started with Mule Cloud connect , and Mule in action .

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