Flow đầu tiên — Hello World API
Mục tiêu bài này
Kết thúc bài này bạn sẽ tạo được một HTTP API:
GET /api/hello→ trả về{"message": "Hello, MuleSoft!"}GET /api/hello?name=Huan→ trả về{"message": "Hello, Huan!"}
Flow là gì?
Flow là đơn vị xử lý cơ bản trong Mule — một pipeline nhận event vào, xử lý qua từng component, rồi trả về kết quả.
Khi HTTP Listener nhận request → tạo Mule Event → event đi qua từng component → response trả về client.
Mule Event — Trái tim của Mule
Mỗi lần có request đến, Mule tạo một Mule Event chứa toàn bộ thông tin:
payload → body của request (sau khi parse)
attributes.headers → {"Content-Type": "application/json", ...}
attributes.queryParams → {"name": "Huan", "page": "1"}
attributes.uriParams → {"id": "123"} (từ path /orders/{id})
attributes.method → "GET"
attributes.requestPath → "/api/hello"
vars.myVariable → giá trị bạn đã set bằng Set Variable
Tạo project mới
- File → New → Mule Project
- Project Name:
hello-api - Mule Runtime: chọn phiên bản mới nhất
- Click Finish
Bước 1 — Thêm HTTP Listener
HTTP Listener là event source — lắng nghe HTTP request đến.
Tạo HTTP Listener Config (dùng chung)
- Tab Global Elements → Create...
- Tìm HTTP Listener config → OK
- Điền:
- Name:
HTTP_Listener_config - Host:
0.0.0.0 - Port:
8081
- Name:
- Click OK
Thêm HTTP Listener vào flow
- Trong Mule Palette, tìm HTTP → kéo Listener vào Canvas
- Trong Properties Panel:
- Connector configuration: chọn
HTTP_Listener_config - Path:
/api/hello - Allowed Methods:
GET
- Connector configuration: chọn
Bước 2 — Thêm Set Payload
Set Payload thay đổi payload của Mule Event — đây là nội dung trả về cho client.
- Trong Palette, tìm Core → Set Payload
- Kéo vào Canvas, sau HTTP Listener
- Trong Properties:
- Value: click icon
fxđể nhập DataWeave expression - Nhập:
output application/json --- {"message": "Hello, MuleSoft!"} - MIME Type:
application/json
- Value: click icon
Flow hiện tại:
[HTTP Listener: GET /api/hello] ──► [Set Payload: {"message": "Hello, MuleSoft!"}]
Bước 3 — Chạy và test
Chạy app
Click chuột phải project → Run As → Mule Application
Đợi Console hiện:
INFO ... **** Mule is up and kicking (listening on: http://0.0.0.0:8081)
Test bằng curl
curl http://localhost:8081/api/hello
Kết quả:
{"message": "Hello, MuleSoft!"}
Test bằng Postman
- Method:
GET - URL:
http://localhost:8081/api/hello - Click Send
Bước 4 — Đọc query parameter
Thêm tính năng: nếu có ?name=Huan thì trả về Hello, Huan!, không có thì trả về Hello, MuleSoft!.
Sửa Set Payload
Click vào Set Payload, đổi Value thành:
output application/json
---
{
message: "Hello, " ++ (attributes.queryParams.name default "MuleSoft") ++ "!"
}
Giải thích:
attributes.queryParams.name— đọc query param?name=default "MuleSoft"— nếu không cónamethì dùng"MuleSoft"++— nối string trong DataWeave
Test lại
# Không có name
curl http://localhost:8081/api/hello
# → {"message": "Hello, MuleSoft!"}
# Có name
curl "http://localhost:8081/api/hello?name=Huan"
# → {"message": "Hello, Huan!"}
Bước 5 — Thêm Logger để debug
Logger giúp in thông tin ra Console khi flow chạy.
- Kéo Logger (trong Core) vào sau HTTP Listener, trước Set Payload
- Trong Properties:
- Message:
#["\n→ Request: " ++ attributes.method ++ " " ++ attributes.requestPath ++ "\n→ QueryParams: " ++ write(attributes.queryParams, "application/json")] - Level:
INFO
- Message:
Flow đầy đủ:
[HTTP Listener] ──► [Logger] ──► [Set Payload]
Khi gọi API, Console sẽ in:
INFO ... → Request: GET /api/hello
→ QueryParams: {"name":"Huan"}
Nhìn vào XML phía sau
Chuyển sang tab Configuration XML để xem file XML mà Studio tạo ra:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
...>
<!-- Global Config — dùng chung -->
<http:listener-config name="HTTP_Listener_config">
<http:listener-connection host="0.0.0.0" port="8081"/>
</http:listener-config>
<!-- Flow -->
<flow name="hello-apiFlow">
<!-- Event Source -->
<http:listener config-ref="HTTP_Listener_config"
path="/api/hello"
allowedMethods="GET"/>
<!-- Logger -->
<logger level="INFO"
message='#["→ Request: " ++ attributes.method]'/>
<!-- Set Payload -->
<set-payload mimeType="application/json">
<![CDATA[%dw 2.0
output application/json
---
{
message: "Hello, " ++ (attributes.queryParams.name default "MuleSoft") ++ "!"
}]]>
</set-payload>
</flow>
</mule>
Bạn có thể chỉnh sửa trực tiếp XML hoặc dùng giao diện kéo thả — cả hai đều hợp lệ.
Mở rộng — Path parameter
Thay vì query param ?name=Huan, dùng path param /api/hello/Huan:
- Sửa HTTP Listener path thành:
/api/hello/{name} - Sửa Set Payload:
output application/json
---
{
message: "Hello, " ++ (attributes.uriParams.name default "MuleSoft") ++ "!"
}
curl http://localhost:8081/api/hello/Huan
# → {"message": "Hello, Huan!"}
Cheat sheet Mule Event
| Truy cập | DataWeave expression |
|---|---|
| Body request | payload |
Query param ?page=2 | attributes.queryParams.page |
Path param /{id} | attributes.uriParams.id |
| HTTP header | attributes.headers['Content-Type'] |
| HTTP method | attributes.method |
| Request URI | attributes.requestPath |
| Variable đã set | vars.myVar |
| Correlation ID | correlationId |
Flow Hello World chỉ trả về dữ liệu cứng. Trong thực tế, bạn cần biến đổi dữ liệu từ nhiều nguồn khác nhau — đó là lúc cần DataWeave.