简体   繁体   English

使用脚本中介调用中介

[英]Call Mediator with Script Mediator

I'm looking for an API with Call Mediator.我正在寻找带有呼叫调解器的 API。 Then I want to process the data coming with Script Mediaor and generate output with Respond.然后我想处理来自 Script Mediaor 的数据并使用 Respond 生成输出。 If I don't use Script Mediator, my stream will work.如果我不使用 Script Mediator,我的流将工作。 But when I want to use Script Mediaor I get the following error.但是当我想使用 Script Mediaor 时,出现以下错误。

Unexpected character in preface '{' (code 123);序言中的意外字符“{”(代码 123); expected '<' in [row,col {unknown source}]: [1,1] [行,列{未知来源}]中的预期“<”:[1,1]

<?xml version="1.0" encoding="UTF-8"?>
<api context="/x" name="x" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <call>
                <endpoint>
                    <http method="get" uri-template="https://jsonplaceholder.typicode.com/users">
                        <suspendOnFailure>
                            <initialDuration>-1</initialDuration>
                            <progressionFactor>-1</progressionFactor>
                            <maximumDuration>0</maximumDuration>
                        </suspendOnFailure>
                        <markForSuspension>
                            <retriesBeforeSuspension>0</retriesBeforeSuspension>
                        </markForSuspension>
                    </http>
                </endpoint>
            </call>
            <script language="js"><![CDATA[var x = mc.getPayloadJSON();

if  (x.id == "1")
mc.setPayloadJSON('{"s":"success"}');
else 
mc.setPayloadJSON('{"s":"error"}');]]></script>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Thanks in advance.提前致谢。

The Script Mediator is a content-aware mediator and when you add a content-aware mediator to the mediation flow the message will be built(Binary message converted into a readable format) and brought into the mediation flow. Script Mediator是一个内容感知中介器,当您将一个内容感知中介器添加到中介流中时,将构建消息(将二进制消息转换为可读格式)并将其带入中介流。 This happens through message builders .这通过消息构建器发生。 From the error snippet you shared, it seems you are receiving a JSON message as a response.从您共享的错误片段中,您似乎收到了一条 JSON 消息作为响应。 So there can be two reasons for this error.所以这个错误可能有两个原因。

  1. As Arunan mentioned, your backend may not be sending the correct Content-Type as a header with the response.正如 Arunan 提到的,您的后端可能没有发送正确的 Content-Type 作为响应的标头。
  2. The correct message builders are not set in the transport configurations to match the Content-Type header the backend is transmitting.未在传输配置中设置正确的消息构建器以匹配后端正在传输的 Content-Type 标头。

If you take the second case, first make sure a message builder is set for the default JSON content-type application/json in the <SERVER_HOME>/conf/axis2/axis2.xml .如果您采用第二种情况,首先确保为<SERVER_HOME>/conf/axis2/axis2.xml中的默认 JSON 内容类型application/json设置了消息构建器。 If it's there check if your backend is sending a JSON variation of a default content-type.如果存在,请检查您的后端是否正在发送默认内容类型的 JSON 变体。 For example "application/ld+json" etc. If that's the case you need to add a new message builder for this content type.例如“application/ld+json”等。如果是这种情况,您需要为此内容类型添加新的消息构建器。 You can refer to this document if you are on MI.如果您在 MI 上,可以参考文档。 If you are on EI refer to this .如果您在 EI 上,请参阅

Update After checking your comments, it looks like a straightforward integration.更新检查您的评论后,它看起来像一个简单的集成。 Other than the above two reasons this can be the backend altogether sending a wrong response.除了上述两个原因之外,这可能是后端完全发送了错误的响应。 Are you using the browser to test this API?您是否使用浏览器来测试此 API? If so you may be getting a different response due to additional headers added by the browser.如果是这样,由于浏览器添加了额外的标头,您可能会收到不同的响应。 Hence please try removing all the transport headers before the call mediator.因此,请尝试在调用中介之前删除所有传输标头。 Refer to the below.请参阅下文。 Note the <property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>注意<property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>

<property action="remove" name="TRANSPORT_HEADERS" scope="axis2"/>
<call>
    <endpoint>
        <http method="get" uri-template="https://jsonplaceholder.typicode.com/users">
            <suspendOnFailure>
                <initialDuration>-1</initialDuration>
                <progressionFactor>-1</progressionFactor>
                <maximumDuration>0</maximumDuration>
            </suspendOnFailure>
            <markForSuspension>
                <retriesBeforeSuspension>0</retriesBeforeSuspension>
            </markForSuspension>
        </http>
    </endpoint>
</call>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM