【问题标题】:Mule - java.lang.String cannot be cast to java.util.MapMule - java.lang.String 无法转换为 java.util.Map
【发布时间】:2016-09-26 13:19:55
【问题描述】:

我想编写一个 mule 应用程序,它将读取数据库中未处理的记录,以 JSON 有效负载格式将它们组合起来,然后访问 REST Web 服务。 我能够从数据库中读取记录并能够将数据库记录转换为 JSON。但是,每当我运行应用程序时,都会出现以下异常

java.lang.ClassCastException: java.lang.String 无法转换为 java.util.Map

这是我的 Mule 配置 XML

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

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:json="http://www.mulesoft.org/schema/mule/json" 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" 
    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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="root" database="my_database_name" doc:name="MySQL Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="example.net" port="8000" basePath="API" doc:name="HTTP Request Configuration"/>
    <flow name="cwg_clientFlow">
        <poll doc:name="Poll">
            <db:select config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[SELECT * FROM cwg_ws_data WHERE SyncFlag = 0]]></db:parameterized-query>
            </db:select>
        </poll>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <json:object-to-json-transformer   doc:name="Object to JSON" />
        <logger message="JSON Payload is #[payload]" level="INFO" doc:name="Logger"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/cwg" method="POST" doc:name="HTTP">
            <http:request-builder>
                <http:query-params expression="#[payload]"/>
                <http:header headerName="access_token" value="MQTgpMUmyQLt134maB6vPp6oWFgMtGsqzIlpCN74"/>
            </http:request-builder>
        </http:request>
        <logger message="webservice response #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

我不明白哪里出了问题

请帮帮我,我从前 2 天开始就在尝试这个。

提前致谢

-Paresh (kendreparesh@gmail.com)

【问题讨论】:

  • 您似乎正在使用有效负载作为 HTTP 请求的查询参数,但为了将它们设置为表达式,它们需要是 Map(查询键/查询值)。这就是铸造错误的意义所在。您实际需要什么作为查询参数?
  • Web 服务需要 JSON 格式的有效负载,因为我正在将数据库行转换为 JSON 格式并尝试将此 JSON 传递给 Web 服务。
  • 那你就不需要查询参数了。

标签: rest mule classcastexception http-request


【解决方案1】:

尝试删除此行。

<http:query-params expression="#[payload]"/>

它应该工作。由于您的有效负载是 Json String,并且您正在尝试将其映射到需要 Map 的查询参数。同样对于 POST,您的有效负载将转换为正文。

【讨论】:

  • 非常感谢。它对我有用:) 但是我将“响应代码 401 映射为失败”作为例外,但我可以从那里得到它。谢谢...
【解决方案2】:

query-params 中的表达式必须是一个映射。如果要将数据作为查询参数传递,为什么要对 json 做对象?尝试删除以下两行 -

<json:object-to-json-transformer   doc:name="Object to JSON" />
        <logger message="JSON Payload is #[payload]" level="INFO" doc:name="Logger"/>

【讨论】:

  • REST weebservice 需要 JSON 格式的有效负载,因为我正在将数据库中的记录转换为 JSON。
  • 好的,在这种情况下,您应该删除查询参数,就像@anupambhusari 说的那样。
猜你喜欢
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多