我在将自定义对象返回到骡子流和如何返回可由另一个骡子流使用的对象时做错了什么



这是我在流程中所做的:

<?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>

这是myMethod的代码。

   public DummyPojo myMethod(String payload) throws IOException {
        // do things with payload.
        DummyPojo pojo = new DummyPojo();
        pojo.setCode(0);
        pojo.setDesc(payload);
        return pojo;
    }

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;
    } 
}

但是,当我运行流时,出现以下错误。

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

关于我在流程中做错了什么的任何想法?我正在尝试返回自定义对象而不是字符串。

编辑:

正在尝试使我创建的对象(DummyPojo)可供另一个骡子流使用。即。例如,子流 B 由于其操作而生成DummyPojo。如何/从 B 返回什么,以便 A 可以使用返回的对象

这里有很多事情并不完全正确:

你有两个实例化的同一个类:com.intuit.platform.fdp.transaction.orchestration.JavaMule。

这在连续调用两次,一次使用组件,一次使用调用。如果JavaMule只有一个具有单个参数字符串签名的方法,那么我将保留组件并删除调用。

串变压器的对象是错误的,就像瑞安说的那样。此外,这可能不是您打算做的。在你的问题中,你提到你想返回一个对象,如果这实际上是你的意图,只需完全删除对象到字符串转换器。如果要以某种方式序列化对象,只需使用 xstream 的对象到 xml 转换器或对象到字节数组转换器。

如果使用 echo 组件是记录有效负载,则应使用 logger 元素:<logger messsage="#[message.payload]" /> echo-component是一个已弃用的 mule 2 组件,它将产生一些不希望的效果,例如将入站属性复制到出站。

编辑

要拥有一个可以从其他流调用的可重用流,您不需要显式执行任何操作,您只需要使用 flow-ref 调用流:

<flow-ref name="javaflowFlow1" />

这将忽略inbound-endpoint(无论如何,您可能不需要问题流程)。

有关本主题的更多信息,请参阅文档、Mule Cloud Connect 入门和 Mule 的实际操作。

您正在调用始终返回String但要求它返回自定义对象的object-to-string-transformer

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

这不起作用,您将看到您看到的错误。如果您不需要字符串,请删除此转换器。

最新更新