NullPayload 中的基本流程结果
Basic flow results in NullPayload
我是 Mule 的新手,我正在努力让 "Mule in Action" 书中的第一个例子发挥作用。
我正在使用 Mule 3.9 和 Anypoint Studio 6.4.1。在第 1 章中,他们描述了我创建的非常基本的 product_registration 流程,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" 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"
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/jms
http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8880" basePath="products" doc:name="HTTP Listener Configuration"/>
<jms:activemq-connector name="Active_MQ" username="admin" password="admin" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="product_registrationFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger Before"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger level="INFO" doc:name="Logger After"/>
<jms:outbound-endpoint doc:name="JMS" queue="products"/>
</flow>
</mule>
以及附带的功能测试:
@Test
public void testCanRegisterProducts() throws Exception {
LocalMuleClient client = muleContext.getClient();
String productAsJson = "{ \"name\":\"Widget\", \"price\": 9.99, \"weight\": 1.0, \"sku\": \"abcd-56789\" }";
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
client.dispatch("http://localhost:8880/products", source);
MuleMessage result = client.request("jms://products", RECEIVE_TIMEOUT);
assertNotNull(result);
assertFalse(result.getPayload() instanceof NullPayload);
assertEquals(productAsJson, result.getPayloadAsString());
}
当我 运行 测试时,它在最后一个断言处失败,因为实际有效载荷是:
{NullPayload}
如果我直接查看 ActiveMQ,我会看到该负载。如果我手动 post 到 Mule(使用像 Chrome 中的 Poster 这样的工具,只设置 header Content-Type: application/json)有效载荷是有效的 JSON 并且我可以让测试通过(因为它正在从 queue post 中获取未决消息,并且它创建的消息位于 queue 的末尾有效载荷 {NullPayload}。
有人可以阐明为什么流程在从 JUnit 测试调用时失败,但在使用 Poster 等工具调用时似乎有效吗?
更新:在 Pierre B 的帮助下,我让它工作了。 FunctionalTestCase 中 MuleMessage 的初始化更新如下:
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
source.setProperty("Content-Type", "application/json", PropertyScope.INBOUND);
source.setProperty("Content-Length", Integer.valueOf(productAsJson.length()), PropertyScope.INBOUND);
你说当你 POST 使用外部工具而不是 MuleClient 发送消息时它有效:
using a tool like Poster in Chrome, setting only the header Content-Type: application/json
尝试将相同的 header 添加到您的 MuleMessage
,例如:
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
# define the headers
Map<String, Object> headers = new HashMap<String, Object>(1);
headers.put("Content-Type", "application/json");
headers.put("Content-Length", sourceLength);
# add the headers as function parameter
client.dispatch("http://localhost:8880/products", source, headers);
编辑:正如@sceaj 所指出的,Content-Type 和Content-Length header 都是必需的。
我是 Mule 的新手,我正在努力让 "Mule in Action" 书中的第一个例子发挥作用。
我正在使用 Mule 3.9 和 Anypoint Studio 6.4.1。在第 1 章中,他们描述了我创建的非常基本的 product_registration 流程,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" 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"
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/jms
http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8880" basePath="products" doc:name="HTTP Listener Configuration"/>
<jms:activemq-connector name="Active_MQ" username="admin" password="admin" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="product_registrationFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger level="INFO" doc:name="Logger Before"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger level="INFO" doc:name="Logger After"/>
<jms:outbound-endpoint doc:name="JMS" queue="products"/>
</flow>
</mule>
以及附带的功能测试:
@Test
public void testCanRegisterProducts() throws Exception {
LocalMuleClient client = muleContext.getClient();
String productAsJson = "{ \"name\":\"Widget\", \"price\": 9.99, \"weight\": 1.0, \"sku\": \"abcd-56789\" }";
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
client.dispatch("http://localhost:8880/products", source);
MuleMessage result = client.request("jms://products", RECEIVE_TIMEOUT);
assertNotNull(result);
assertFalse(result.getPayload() instanceof NullPayload);
assertEquals(productAsJson, result.getPayloadAsString());
}
当我 运行 测试时,它在最后一个断言处失败,因为实际有效载荷是:
{NullPayload}
如果我直接查看 ActiveMQ,我会看到该负载。如果我手动 post 到 Mule(使用像 Chrome 中的 Poster 这样的工具,只设置 header Content-Type: application/json)有效载荷是有效的 JSON 并且我可以让测试通过(因为它正在从 queue post 中获取未决消息,并且它创建的消息位于 queue 的末尾有效载荷 {NullPayload}。
有人可以阐明为什么流程在从 JUnit 测试调用时失败,但在使用 Poster 等工具调用时似乎有效吗?
更新:在 Pierre B 的帮助下,我让它工作了。 FunctionalTestCase 中 MuleMessage 的初始化更新如下:
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
source.setProperty("Content-Type", "application/json", PropertyScope.INBOUND);
source.setProperty("Content-Length", Integer.valueOf(productAsJson.length()), PropertyScope.INBOUND);
你说当你 POST 使用外部工具而不是 MuleClient 发送消息时它有效:
using a tool like Poster in Chrome, setting only the header Content-Type: application/json
尝试将相同的 header 添加到您的 MuleMessage
,例如:
MuleMessage source = new DefaultMuleMessage(productAsJson, muleContext);
# define the headers
Map<String, Object> headers = new HashMap<String, Object>(1);
headers.put("Content-Type", "application/json");
headers.put("Content-Length", sourceLength);
# add the headers as function parameter
client.dispatch("http://localhost:8880/products", source, headers);
编辑:正如@sceaj 所指出的,Content-Type 和Content-Length header 都是必需的。