Mulesoft 简明教程
MuleSoft - Web Services Using Anypoint Studio
REST Web Service
REST 的全称是表述性状态转移,它与 HTTP 绑定。因此,如果您想设计一个专门在网络上使用的应用程序,REST 是最佳选择。
The full form of REST is Representational State Transfer which is bound with HTTP. Hence, if you want to design an application to be used exclusively on the web, REST is the best option.
Consuming RESTful Web Services
在以下示例中,我们将使用 REST 组件和 Mule Soft 提供的名为 American Flights details 的一个公共 RESTful 服务。它有各种详细信息,但我们将使用 GET: http://training-american-ws.cloudhub.io/api/flights 来返回所有航班详细信息。如前所述,REST 与 HTTP 绑定,因此我们也需要两个 HTTP 组件——一个侦听器和一个请求,用于此应用程序。以下屏幕截图显示了 HTTP 侦听器的配置 -
In the following example, we will be using REST component and one public RESTful service provided by Mule Soft called American Flights details. It has various details but we are going to use GET:http://training-american-ws.cloudhub.io/api/flights that will return all flight details. As discussed earlier, REST is bound with HTTP, hence we need two HTTP components ― one is Listener and other is Request, for this application too. Below screenshot shows the configuration for HTTP listener −
Configuring and passing arguments
HTTP 请求的配置如下 -
The configuration for HTTP request is given below −
现在,根据我们的工作空间流程,我们采用了记录器,因此可以按如下方式对其进行配置 -
Now, as per our workspace flow, we have taken logger so it can be configured as below −
在消息选项卡中,我们编写代码将有效负载转换为字符串。
In the message tab, we write code to convert the payload into strings.
Testing the Application
现在,保存并运行该应用程序,然后转到 POSTMAN 中以检查最终输出,如下所示 -
Now, save and run the application and go to POSTMAN to check the final output as shown below −
您可以看到它通过使用 REST 组件给出了航班详细信息。
You can see it gives the flight details by using REST component.
SOAP Component
SOAP 的全称为 Simple Object Access Protocol 。它基本上是用于在 Web 服务实施中交换信息的通信协议规范。接下来,我们将在 Anypoint Studio 中使用 SOAP API 来使用 Web 服务访问信息。
The full form of SOAP is Simple Object Access Protocol. It is basically a messaging protocol specification for exchanging information in the implementation of web services. Next, we are going to use SOAP API in Anypoint Studio to access the information using web services.
Consuming SOAP-based Web Services
对于此示例,我们将使用名为国家/地区信息服务的公共 SOAP 服务,该服务保留与国家/地区信息相关的服务。其 WSDL 地址为: http://www.oorsprong.org/websamples.countryinfo/countryinfoservice.wso?WSDL
For this example, we are going to use public SOAP service whose name is Country Info Service which retains the services related to country information. Its WSDL address is: http://www.oorsprong.org/websamples.countryinfo/countryinfoservice.wso?WSDL
首先,我们需要从 Mule Palette 的画布中拖动 SOAP 消费,如下所示:
First, we need to drag SOAP consume in our canvas from Mule Palette as shown below −
Configuring and Passing Arguments
接下来,我们需要配置 HTTP 请求,如下面所示,如上一个示例中所做:
Next, we need to configure HTTP request as done in above example as given below −
现在,我们还需要配置 Web 服务使用者,如下所示:
Now, we also need to configure the Web Service Consumer as shown below −
在 WSDL 位置的地方,我们需要提供 WSDL 的 Web 地址,如上所述(此示例)。提供 Web 地址后,Studio 会自行搜索服务、端口和地址。您无需手动提供它。
At the place of WSDL Location, we need to provide the web address of WSDL, which is provided above (for this example). Once you give the web address, Studio will search for service, Port and Address by itself. You need not provide it manually.
Testing the Application
保存并运行应用程序,然后转到 Google Chrome 检查最终输出。键入 http://localhist:8081/helloSOAP (此示例),它将显示如下面的屏幕截图所示的国家/地区名称(按代码):
Save and run the application and go to Google Chrome for checking the final output. Type http://localhist:8081/helloSOAP (for this example) and it will show the country name by code as shown in the screenshot below −