我是Mule ESB的新手,并尝试向REST服务发送帖子请求。请求的主体是String形式,请求应该是x-www-form-urlencoded。我将有效负载设置为我的参数并将请求发送到REST服务,但我得到一个例外:
Failed to invoke REST service "http://username:password@192.168.10.252/api/rest/session/login.json". Message payload is of type: String
我的其余网址是:(#[restURL])$ {dms.host} /api/rest/session/login.json
我的参数是(设置为有效负载#[restBody]):username = user& password = pass
<sub-flow name="RESTrequestToDMS" doc:name="RESTrequestToDMS" processingStrategy="synchronous">
<set-variable variableName="originalMessage" value="#[payload]" doc:name="Backup original message"/>
<logger message="#[restBody]" level="INFO" doc:name="Logger"/>
<choice doc:name="Choice">
<when expression="restHTTPmethod == 'POST'">
<processor-chain>
<set-payload value="#[restBody]" doc:name="Set Payload"/>
<http:rest-service-component httpMethod="POST" serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]"></http:rest-service-component>
</processor-chain>
</when>
<when expression="restHTTPmethod == 'GET'">
<processor-chain>
<http:rest-service-component httpMethod="GET" serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]?#[restBody]"></http:rest-service-component>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="Unknown http method type is provided! " level="ERROR" doc:name="Logger"/>
</processor-chain>
</otherwise>
</choice>
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<echo-component doc:name="Echo"/>
<set-variable variableName="RESTResponse" value="#[payload]" doc:name="set RESTResponse"/>
<set-payload value="#[flowVars['originalMessage']]" doc:name="Restore Original Message"/>
</sub-flow>
有人可以帮我解决这个问题吗?
提前多多感谢。
注意:根据David的回复更新。
------------------- UPDATE --------------------
所以现在,出于测试目的,我已经修改了这样的流程:
<set-payload value="#[restBody]" doc:name="Set Payload"/>
<custom-transformer class="de.mfg.osii.http.StringToURIParametersMapTransformer" doc:name="String To URI Parameters"/>
<http:rest-service-component httpMethod="POST" serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]">
<http:requiredParameter key="username" value="#[payload.get('username')]" />
<http:requiredParameter key="password" value="#[payload.get('password')]" />
</http:rest-service-component>
所以现在有效载荷是:{username = user,password = pass} 但是现在当我尝试运行它时,我收到以下错误:
1. Failed to invoke REST service "http://user:pass@192.168.10.252/api/rest/session/login.json". Message payload is of type: LinkedHashMap (org.mule.transport.http.components.RestServiceException)
更新:上述问题(例外)刚刚通过添加值为“application / x-www-form-urlencoded”的消息属性“Content-Type”来解决。 / p>
下面的最后一部分对我来说仍然是个谜。)
另一件事是,正如我在David回答的评论中所述,我是否可以自适应方式为其余组件添加所需的参数?为了更清楚,我不想为每个不同的请求创建不同的流。我在有效负载中有参数映射,所以我想迭代它们并将它们放在所需的参数中。这样的事情可能吗?
提前多多感谢!
------------------- UPDATE --------------------
所以,我修改了我的流程并使用了大卫的建议。但是,现在,我收到HTTP 405错误(不允许方法)。但我确定我使用正确的内容类型发布到正确的地址。这是什么原因?有人有想法吗?
进入此流程的我的有效负载是一个字符串(网址编码参数),例如 - &gt;
的sessionId = 9eub9gm7k7oc1ub81dhef6t46q&安培;!XML =%3CObjectList%3E%3CFolderObject%3E%3CAddToFolder%20RefType%3D%22Path%22%20ClassName%3D%22FolderObject%22%3E%3C%5BCDATA%5B%2FfolderPath%5D%5D %3E%3C%2FAddToFolder%3E%3CName%3E%3C%!5BCDATA%5Baaa%5D%5D%3E%3C%2FName%3E%3CDescription%3E%3C%!5BCDATA%5BRequests%20from%20user%20aaa%20顷%20stored%20英寸%20this%20folder%5D%5D%3E%3C%2FDescription%3E%3CNoErrorIfExist%2F%3E%3C%2FFolderObject%3E%3C%2FObjectList%3E
<flow name="RESTrequestToDMS" doc:name="RESTrequestToDMS" processingStrategy="synchronous">
<set-variable variableName="originalMessage" value="#[payload]" doc:name="Backup original message"/>
<choice doc:name="Choice">
<when expression="restHTTPmethod == 'POST'">
<processor-chain>
<set-payload value="#[restBody]" doc:name="Set Payload"/>
<logger message="The payload before http post is: #[message.payload]" level="INFO" doc:name="Logger"/>
<http:outbound-endpoint exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]" user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
</http:outbound-endpoint>
</processor-chain>
</when>
<when expression="restHTTPmethod == 'GET'">
<processor-chain>
<http:outbound-endpoint method="GET" exchange-pattern="request-response" doc:name="HTTP" address="http://#[restURL]?#[restBody]" user="${dms.user}" password="${dms.pass}" contentType="application/x-www-form-urlencoded">
</http:outbound-endpoint>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="Unknown http method type is provided! " level="ERROR" doc:name="Logger"/>
</processor-chain>
</otherwise>
</choice>
<echo-component doc:name="Echo"/>
<choice doc:name="Choice">
<when expression="payload != null && payload != empty && payload != ''">
<processor-chain>
<json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.Map"/>
<set-variable variableName="RESTResponse" value="#[payload]" doc:name="set RESTResponse"/>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="The payload is null or empty! HTTP response code is: #[message.inboundProperties['http.status']]" level="INFO" doc:name="Logger"/>
<set-variable variableName="RESTResponse" value="#[message.inboundProperties['http.status']]" doc:name="Set HTTP Status Code"/>
</processor-chain>
</otherwise>
</choice>
<set-payload value="#[flowVars['originalMessage']]" doc:name="Restore Original Message"/>
</flow>
答案 0 :(得分:2)
以下是使用rest-service-component
传递参数进行URL编码的方法:
<http:rest-service-component httpMethod="POST"
serviceUrl="http://${dms.user}:${dms.pass}@#[restURL]">
<http:requiredParameter key="username" value="user" />
<http:requiredParameter key="password" value="pass" />
</http:rest-service-component>