我正在尝试使用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客户端吗?
如果要在使用Mule SDK创建的自定义模块中实现HTTP请求,则必须按照文档中的描述使用HTTP客户端:https://docs.mulesoft.com/mule-sdk/1.1/HTTP-based-connectors#use-the-mule-http-client
您没有提供任何理由或需要在自定义模块中实现请求。仅使用自定义策略中的HTTP请求程序执行HTTP请求会更容易。