将JSON对象作为有效负载

时间:2015-06-09 16:48:44

标签: json mule

当我尝试更简单的事情时,这是一个我以前没有遇到过的问题。我试图使用Mule向Slack发送消息,这样做我首先尝试将一个JSON对象发送到我的特定松弛组的hook.slack.com地址。我这样做了,它完全按照预期工作(虽然显然JSON只是ESB上的静态内容)。这看起来像:

<?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">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="hooks.slack.com" doc:name="HTTP Request Configuration"/>
    <flow name="testFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-payload value="{&quot;channel&quot;: &quot;#general&quot;, &quot;username&quot;: &quot;test&quot;, &quot;text&quot;: &quot;How's this?&quot;, &quot;icon_emoji&quot;: &quot;:man_with_turban:&quot;}" doc:name="Set Payload"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/services/myslackaddress" method="POST" doc:name="HTTP"/>
    </flow>
</mule>

正如前面提到的,这种工作得很好,可以获得JSON,并按照预期的方式将消息发布到给定的组中。然后,我通过创建一个带有IMAP连接器的ESB,获取发送给它的任何电子邮件,并将该电子邮件的正文粘贴到松弛状态,从而对此进行了稍微扩展。这似乎是一个简单的改变,我所做的就是这样(请忽略日志,他们只是为了调试而显然):

<imaps:connector name="IMAP" validateConnections="true" checkFrequency="300" doc:name="IMAP">
</imaps:connector>
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="hooks.slack.com"  doc:name="HTTP Request Configuration"/>
<flow name="Global_ResourcesFlow">
    <imaps:inbound-endpoint host="imap.gmail.com" port="993" user="testemail" password="password" connector-ref="IMAP" responseTimeout="10000" doc:name="Grab email sent to testemail"/>
    <email:email-to-string-transformer doc:name="Convert body of email to string"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    <set-variable variableName="emailBody" value="#[payload]" doc:name="Set body text"/>
    <set-payload value="{&quot;channel&quot;: &quot;#general&quot;, &quot;username&quot;: &quot;Bot&quot;, &quot;text&quot;: &quot;#[flowVars.emailBody]&quot;, &quot;icon_emoji&quot;, &quot;:heart_eyes_cat:&quot;}" doc:name="Set Payload"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    <http:request config-ref="HTTP_Request_Configuration" path="/services/myslackaddress" method="POST" doc:name="HTTP">
        <http:request-builder>
        </http:request-builder>
    </http:request>
</flow>

这似乎只是对我的一个小改动,但由于某种原因,现在当我访问最终的http端点时,我在Mule控制台中遇到某种错误,如下所示:

ERROR 2015-06-09 12:32:47,462 [[slack-notifications-esb].Global_ResourcesFlow.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Response code 500 mapped as failure. Message payload is of type: BufferInputStream
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. Response code 500 mapped as failure. Message payload is of type: BufferInputStream (org.mule.module.http.internal.request.ResponseValidatorException)
  org.mule.module.http.internal.request.SuccessStatusCodeValidator:37 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/module/http/internal/request/ResponseValidatorException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.module.http.internal.request.ResponseValidatorException: Response code 500 mapped as failure. Message payload is of type: BufferInputStream
    at org.mule.module.http.internal.request.SuccessStatusCodeValidator.validate(SuccessStatusCodeValidator.java:37)
    at org.mule.module.http.internal.request.DefaultHttpRequester.innerProcess(DefaultHttpRequester.java:202)
    at org.mule.module.http.internal.request.DefaultHttpRequester.process(DefaultHttpRequester.java:166)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

它甚至更奇怪,因为正在发送的最终有效负载与在另一个工作正常的有效负载相同,这只是一些有效的JSON,如下所示:

{"channel": "#general", "username": "Bot", "text": "test", "icon_emoji", ":heart_eyes_cat:"}

1 个答案:

答案 0 :(得分:2)

添加:

<http:header headerName="Content-Type" value="application/json" />
<{1>}中的

指定请求正文是JSON实体。