【问题标题】:Call external API using Mule SDK使用 Mule SDK 调用外部 API
【发布时间】:2020-03-14 16:47:31
【问题描述】:

我正在尝试使用 Mule SDK 实施 Mule 4.x 策略。为此,我需要在策略操作实现中调用外部 API。外部 API 响应返回的结果将决定策略输出。

public class MyMulePolicyOperations
{
    @MediaType( value = ANY, strict = false )
    public void handle(
            @Config MyMulePolicyConfiguration configuration,
            @Alias( "httpRequestMethod" ) String httpRequestMethod,
            CompletionCallback<String, HttpResponse> callback ) throws Exception
    {
        HttpResponseBuilder httpResponseBuilder = HttpResponse.builder();

        String result = // call an external API and extract "result" from the response

        if ( result.equals( configuration.getMyConfigValue() ) )
        {
            httpResponseBuilder.addHeader( "allow_request", "true" );
        }
        else
        {
            httpResponseBuilder.addHeader( "allow_request", "false" );
        }

        Result<String, HttpResponse> result = Result.<String, HttpResponse> builder()
                .attributes( httpResponseBuilder.build() )
                .build();

        callback.success( result );
    }

}

谁能告诉我如何使用 Mule SDK 实现 REST 客户端?

【问题讨论】:

    标签: rest api rest-client mulesoft mule-sdk


    【解决方案1】:

    如果您想在使用 Mule SDK 创建的自定义模块中实现 HTTP 请求,则必须使用文档中描述的 HTTP 客户端:https://docs.mulesoft.com/mule-sdk/1.1/HTTP-based-connectors#use-the-mule-http-client

    您没有提供任何理由或需要在自定义模块中实现请求。使用自定义策略中的 HTTP 请求器执行 HTTP 请求会更容易。

    【讨论】:

    • 谢谢aled。如上所述,我正在使用 Mule 编写策略并使用 SDK。在策略中,我需要调用外部 API。我试图找出使用 SDK 可以如何简单地完成此操作。
    • 嗨,是的,你提到了它。目前尚不清楚您为什么要以这种方式使用询问。为什么不直接使用自定义策略?
    • 是的,我正在使用自定义策略。在自定义策略本身中,我需要调用外部 API 来验证某些内容。您是否建议我可以在不使用 Mule SDK 的情况下做到这一点?如果是,那将完美无缺。能详细点吗?
    • 是的,您只需要在策略中添加一个 HTTP 请求和配置,类似于在流中完成的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2022-07-06
    • 2018-06-26
    • 1970-01-01
    • 2019-08-13
    相关资源
    最近更新 更多