如何在WSO2中将JSON正文转换为文本

时间:2019-05-16 06:30:45

标签: wso2 wso2esb wso2ei

我正在尝试创建一个包含要在正文中发送的JSON的字符串属性。

问题是,如果我发送的标头为“ Content-Type”的json:“ text / plain”,则是完美的。但是,如果我使用“ Content-Type”:“ application / json”发送它,我认为它会尝试将其转换为xml。

这是用于Linux服务器wso2 EI 6.4.0

这是我要发送的JSON:
{ "hello": "isJson" }

,并且在字符串中带有标头Content-Type:'application / json',我只会得到:isJson

但是当我使用Content-Type:'text / plain'发出请求时,结果是:{“ hello”:“ isJson”}。

如何使用Content-Type获得相同的结果:application / json?

谢谢

1 个答案:

答案 0 :(得分:1)

发生这种情况是因为EI在内部表示XML格式的有效负载。 当您使用text / plain EI进行调用时,将其放在两个“ text”标签中,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><text xmlns="http://ws.apache.org/commons/ns/payload">{ "hello": "isJson" }</text></soapenv:Body></soapenv:Envelope>

请注意,将保留{“ hello”:“ isJson”}。

当您使用application / json进行调用时,EI如下将其转换为XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><hello>isJson</hello></jsonObject></soapenv:Body></soapenv:Envelope>

当您丰富实体的属性时,两个不同的表示形式就会像上述那样丰富到实体。

我们通过向Enrich中介程序引入了本机JSON支持,对该消息流进行了一些改进。您可以从即将发布的EI 6.5.0中获得预期的行为。